Restore UNNCESSARY DEFINITION errors for top-level defs

Non-top-level defs are already covered
This commit is contained in:
Agus Zubiaga 2024-10-30 18:53:41 -03:00
parent a4296ca19d
commit bc0cfef128
No known key found for this signature in database
4 changed files with 80 additions and 1 deletions

View file

@ -1267,6 +1267,14 @@ fn canonicalize_value_defs<'a>(
output = temp_output.output; output = temp_output.output;
if let (PatternType::TopLevelDef, DefKind::Ignored(_)) =
(pattern_type, temp_output.def.kind)
{
env.problems.push(Problem::NoIdentifiersIntroduced(
temp_output.def.loc_pattern.region,
))
}
defs.push(Some(temp_output.def)); defs.push(Some(temp_output.def));
def_ordering.insert_symbol_references(def_id as u32, &temp_output.references) def_ordering.insert_symbol_references(def_id as u32, &temp_output.references)

View file

@ -10185,6 +10185,18 @@ All branches in an `if` must have the same type!
Since these variables have the same name, it's easy to use the wrong Since these variables have the same name, it's easy to use the wrong
one by accident. Give one of them a new name. one by accident. Give one of them a new name.
UNNECESSARY DEFINITION in /code/proj/Main.roc
This destructure assignment doesn't introduce any new variables:
5 main = \n -> n + 2
^^^^
If you don't need to use the value on the right-hand side of this
assignment, consider removing the assignment. Since effects are not
allowed at the top-level, assignments that don't introduce variables
cannot affect a program's behavior
"### "###
); );
@ -10890,7 +10902,55 @@ All branches in an `if` must have the same type!
Foo = Foo Foo = Foo
"# "#
), ),
@"" @r###"
UNNECESSARY DEFINITION in /code/proj/Main.roc
This destructure assignment doesn't introduce any new variables:
3 Pair _ _ = Pair 0 1
^^^^^^^^
If you don't need to use the value on the right-hand side of this
assignment, consider removing the assignment. Since effects are not
allowed at the top-level, assignments that don't introduce variables
cannot affect a program's behavior
UNNECESSARY DEFINITION in /code/proj/Main.roc
This destructure assignment doesn't introduce any new variables:
5 _ = Pair 0 1
^
If you don't need to use the value on the right-hand side of this
assignment, consider removing the assignment. Since effects are not
allowed at the top-level, assignments that don't introduce variables
cannot affect a program's behavior
UNNECESSARY DEFINITION in /code/proj/Main.roc
This destructure assignment doesn't introduce any new variables:
7 {} = {}
^^
If you don't need to use the value on the right-hand side of this
assignment, consider removing the assignment. Since effects are not
allowed at the top-level, assignments that don't introduce variables
cannot affect a program's behavior
UNNECESSARY DEFINITION in /code/proj/Main.roc
This destructure assignment doesn't introduce any new variables:
9 Foo = Foo
^^^
If you don't need to use the value on the right-hand side of this
assignment, consider removing the assignment. Since effects are not
allowed at the top-level, assignments that don't introduce variables
cannot affect a program's behavior
"###
); );
test_report!( test_report!(

View file

@ -195,6 +195,7 @@ pub enum Problem {
unbound_symbol: Symbol, unbound_symbol: Symbol,
region: Region, region: Region,
}, },
NoIdentifiersIntroduced(Region),
OverloadedSpecialization { OverloadedSpecialization {
overload: Region, overload: Region,
original_opaque: Symbol, original_opaque: Symbol,
@ -317,6 +318,7 @@ impl Problem {
Problem::ImplementsNonRequired { .. } => Warning, Problem::ImplementsNonRequired { .. } => Warning,
Problem::DoesNotImplementAbility { .. } => RuntimeError, Problem::DoesNotImplementAbility { .. } => RuntimeError,
Problem::NotBoundInAllPatterns { .. } => RuntimeError, Problem::NotBoundInAllPatterns { .. } => RuntimeError,
Problem::NoIdentifiersIntroduced(_) => Warning,
Problem::OverloadedSpecialization { .. } => Warning, // Ideally, will compile Problem::OverloadedSpecialization { .. } => Warning, // Ideally, will compile
Problem::UnnecessaryOutputWildcard { .. } => Warning, Problem::UnnecessaryOutputWildcard { .. } => Warning,
// TODO: sometimes this can just be a warning, e.g. if you have [1, .., .., 2] but we // TODO: sometimes this can just be a warning, e.g. if you have [1, .., .., 2] but we
@ -480,6 +482,7 @@ impl Problem {
} }
| Problem::NotAnAbility(region) | Problem::NotAnAbility(region)
| Problem::ImplementsNonRequired { region, .. } | Problem::ImplementsNonRequired { region, .. }
| Problem::NoIdentifiersIntroduced(region)
| Problem::DoesNotImplementAbility { region, .. } | Problem::DoesNotImplementAbility { region, .. }
| Problem::OverloadedSpecialization { | Problem::OverloadedSpecialization {
overload: region, .. overload: region, ..

View file

@ -1189,6 +1189,14 @@ pub fn can_problem<'b>(
]); ]);
title = "NAME NOT BOUND IN ALL PATTERNS".to_string(); title = "NAME NOT BOUND IN ALL PATTERNS".to_string();
} }
Problem::NoIdentifiersIntroduced(region) => {
doc = alloc.stack([
alloc.reflow("This destructure assignment doesn't introduce any new variables:"),
alloc.region(lines.convert_region(region), severity),
alloc.reflow("If you don't need to use the value on the right-hand side of this assignment, consider removing the assignment. Since effects are not allowed at the top-level, assignments that don't introduce variables cannot affect a program's behavior"),
]);
title = "UNNECESSARY DEFINITION".to_string();
}
Problem::OverloadedSpecialization { Problem::OverloadedSpecialization {
ability_member, ability_member,
overload, overload,