Skip to content

Commit 86b1fdc

Browse files
giacomocavalierilpil
authored andcommitted
remove additional labels from error
1 parent 3e892ac commit 86b1fdc

9 files changed

+71
-66
lines changed

compiler-core/src/ast/typed.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,11 @@ impl TypedExpr {
10331033
}
10341034
}
10351035

1036-
pub fn var_constructor(&self) -> Option<&ValueConstructor> {
1036+
pub fn var_constructor(&self) -> Option<(&ValueConstructor, &EcoString)> {
10371037
match self {
1038-
TypedExpr::Var { constructor, .. } => Some(constructor),
1038+
TypedExpr::Var {
1039+
constructor, name, ..
1040+
} => Some((constructor, name)),
10391041
_ => None,
10401042
}
10411043
}

compiler-core/src/type_/environment.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,7 @@ pub struct Environment<'a> {
7878
/// local_variable_usages is a stack of scopes. When a local variable is created it is
7979
/// added to the top scope. When a local variable is used we crawl down the scope
8080
/// stack for a variable with that name and mark it as used.
81-
///
82-
/// NOTE: The usize in the tuple here tracks the number of times the variable has
83-
/// been used.
84-
pub local_variable_usages: Vec<HashMap<EcoString, (VariableOrigin, SrcSpan, usize)>>,
85-
86-
/// Used to determine if an argument is only used unchanged in recursive
87-
/// calls, and could thus be removed.
88-
/// For each of the function's arguments keeps track of the usages in
89-
/// recursive calls.
90-
///
91-
pub recursive_argument_usages: HashMap<SrcSpan, Vec<SrcSpan>>,
81+
pub local_variable_usages: Vec<HashMap<EcoString, VariableUsage>>,
9282

9383
/// Used to determine if all functions/constants need to support the current
9484
/// compilation target.
@@ -104,6 +94,14 @@ pub struct Environment<'a> {
10494
pub dev_dependencies: &'a HashSet<EcoString>,
10595
}
10696

97+
#[derive(Debug)]
98+
pub struct VariableUsage {
99+
origin: VariableOrigin,
100+
location: SrcSpan,
101+
usages: usize,
102+
used_recursively: bool,
103+
}
104+
107105
impl<'a> Environment<'a> {
108106
pub fn new(
109107
EnvironmentArguments {
@@ -143,7 +141,6 @@ impl<'a> Environment<'a> {
143141
importable_modules,
144142
current_module,
145143
local_variable_usages: vec![HashMap::new()],
146-
recursive_argument_usages: HashMap::new(),
147144
target_support,
148145
names,
149146
module_type_aliases: HashMap::new(),
@@ -668,30 +665,63 @@ impl Environment<'_> {
668665
location: SrcSpan,
669666
problems: &mut Problems,
670667
) {
671-
if let Some((origin, location, 0)) = self
668+
if let Some(VariableUsage {
669+
origin,
670+
location,
671+
usages: 0,
672+
used_recursively,
673+
}) = self
672674
.local_variable_usages
673675
.last_mut()
674676
.expect("Attempted to access non-existent entity usages scope")
675-
.insert(name.clone(), (origin, location, 0))
677+
.insert(
678+
name.clone(),
679+
VariableUsage {
680+
origin,
681+
location,
682+
usages: 0,
683+
used_recursively: false,
684+
},
685+
)
676686
{
677687
// an entity was overwritten in the top most scope without being used
678688
let mut unused = HashMap::with_capacity(1);
679-
let _ = unused.insert(name, (origin, location, 0));
689+
let _ = unused.insert(
690+
name,
691+
VariableUsage {
692+
origin,
693+
location,
694+
usages: 0,
695+
used_recursively,
696+
},
697+
);
680698
self.handle_unused_variables(unused, problems);
681699
}
682700
}
683701

684702
/// Increments an entity's usage in the current or nearest enclosing scope
685703
pub fn increment_usage(&mut self, name: &EcoString) {
686-
let name = name.clone();
704+
if let Some(VariableUsage { usages, .. }) = self
705+
.local_variable_usages
706+
.iter_mut()
707+
.rev()
708+
.find_map(|scope| scope.get_mut(name))
709+
{
710+
*usages += 1;
711+
}
712+
}
687713

688-
if let Some((_, _, used)) = self
714+
/// Marks an argument as being passed recursively to a function call.
715+
pub fn set_used_recursively(&mut self, name: &EcoString) {
716+
if let Some(VariableUsage {
717+
used_recursively, ..
718+
}) = self
689719
.local_variable_usages
690720
.iter_mut()
691721
.rev()
692-
.find_map(|scope| scope.get_mut(&name))
722+
.find_map(|scope| scope.get_mut(name))
693723
{
694-
*used += 1;
724+
*used_recursively = true;
695725
}
696726
}
697727

@@ -805,24 +835,24 @@ impl Environment<'_> {
805835

806836
fn handle_unused_variables(
807837
&mut self,
808-
unused: HashMap<EcoString, (VariableOrigin, SrcSpan, usize)>,
838+
unused: HashMap<EcoString, VariableUsage>,
809839
problems: &mut Problems,
810840
) {
811-
for (origin, location, usages) in unused.into_values() {
841+
for VariableUsage {
842+
origin,
843+
location,
844+
usages,
845+
used_recursively,
846+
} in unused.into_values()
847+
{
812848
if usages == 0 {
813849
problems.warning(Warning::UnusedVariable { location, origin });
814850
}
815851
// If the function parameter is actually used somewhere, but all the
816852
// usages are just passing it along in a recursive call, then it
817853
// counts as being unused too.
818-
else if origin.is_function_parameter()
819-
&& let Some(recursive_usages) = self.recursive_argument_usages.get(&location)
820-
&& recursive_usages.len() == usages
821-
{
822-
problems.warning(Warning::UnusedRecursiveArgument {
823-
location,
824-
usages: recursive_usages.clone(),
825-
});
854+
else if origin.is_function_parameter() && used_recursively {
855+
problems.warning(Warning::UnusedRecursiveArgument { location });
826856
}
827857
}
828858
}

compiler-core/src/type_/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,6 @@ pub enum Warning {
11101110
///
11111111
UnusedRecursiveArgument {
11121112
location: SrcSpan,
1113-
usages: Vec<SrcSpan>,
11141113
},
11151114
}
11161115

compiler-core/src/type_/expression.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,15 +4467,10 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
44674467
// If the argument is a simple usage of a parameter defined
44684468
// by this same function we want to track it, to report unused
44694469
// parameters that are just passed along the recursive calls.
4470-
if let Some(definition_location) =
4470+
if let Some(name) =
44714471
self.argument_used_in_recursive_call(fun, &inferred, argument_index)
44724472
{
4473-
let _ = self
4474-
.environment
4475-
.recursive_argument_usages
4476-
.entry(definition_location)
4477-
.and_modify(|usages| usages.push(inferred.location()))
4478-
.or_insert(vec![inferred.location()]);
4473+
self.environment.set_used_recursively(&name);
44794474
}
44804475
inferred
44814476
}
@@ -4506,20 +4501,20 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
45064501
called_function: &TypedExpr,
45074502
argument: &TypedExpr,
45084503
argument_index: usize,
4509-
) -> Option<SrcSpan> {
4510-
let argument = argument.var_constructor()?;
4504+
) -> Option<EcoString> {
4505+
let (argument, name) = argument.var_constructor()?;
45114506
let (function_defining_argument, index_in_argument_list) =
45124507
argument.variant.argument_function_and_index()?;
45134508

45144509
let (module, function) = called_function
45154510
.var_constructor()
4516-
.and_then(|constructor| constructor.variant.function_module_and_name())?;
4511+
.and_then(|(constructor, _)| constructor.variant.function_module_and_name())?;
45174512

45184513
if self.environment.current_module == module
45194514
&& function == function_defining_argument
45204515
&& argument_index == index_in_argument_list
45214516
{
4522-
Some(argument.definition_location().span)
4517+
Some(name.clone())
45234518
} else {
45244519
None
45254520
}

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__unused_recursive_function_argument.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ warning: Unused function argument
1515
1616
2 │ pub fn main(x: Int) {
1717
^ This argument is unused
18-
3main(x)
19-
-
2018

2119
This argument is passed to the function when recursing, but it's never used
2220
for anything.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__unused_recursive_function_argument_2.snap

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ warning: Unused function argument
1818
1919
2 │ pub fn main(x, times) {
2020
^ This argument is unused
21-
3case times {
22-
40 -> main(x, 0)
23-
-
24-
5_ -> main(x, times - 1)
25-
-
2621

2722
This argument is passed to the function when recursing, but it's never used
2823
for anything.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__unused_recursive_function_inside_anonymous_function.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ warning: Unused function argument
1616
1717
2 │ pub fn main(x: Int) {
1818
^ This argument is unused
19-
3let _useless = fn(_) { main(x) }
20-
-
2119

2220
This argument is passed to the function when recursing, but it's never used
2321
for anything.

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__unused_recursive_function_with_shadowing.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ warning: Unused function argument
1616
1717
2 │ pub fn main(x: Int) {
1818
^ This argument is unused
19-
3let _useless = fn(x) { main(x) }
20-
4main(x)
21-
-
2219

2320
This argument is passed to the function when recursing, but it's never used
2421
for anything.

compiler-core/src/warning.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Hint: You can safely remove it.
719719
}),
720720
},
721721

722-
type_::Warning::UnusedRecursiveArgument { location, usages } => Diagnostic {
722+
type_::Warning::UnusedRecursiveArgument { location } => Diagnostic {
723723
title: "Unused function argument".into(),
724724
text: wrap(
725725
"This argument is passed to the function when recursing, \
@@ -734,16 +734,7 @@ but it's never used for anything.",
734734
text: Some("This argument is unused".into()),
735735
span: *location,
736736
},
737-
extra_labels: usages
738-
.iter()
739-
.map(|usage| ExtraLabel {
740-
src_info: None,
741-
label: diagnostic::Label {
742-
text: None,
743-
span: *usage,
744-
},
745-
})
746-
.collect(),
737+
extra_labels: vec![],
747738
}),
748739
},
749740

0 commit comments

Comments
 (0)