Skip to content

Commit 216fbd5

Browse files
giacomocavalierilpil
authored andcommitted
stop suggesting to use a value discarded in a different function
1 parent 0460c23 commit 216fbd5

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
- The compiler will now raise warning for inefficient use of `list.length()`
7676
when trying to check is list empty via `0 < list.length(list)` or
77-
`list.length(list) > 0` as well as in other cases. For example, the following
77+
`list.length(list) > 0` as well as in other cases. For example, the following
7878
code:
7979

8080
```gleam
@@ -375,3 +375,7 @@
375375
- Fixed a bug where adding `echo` to the subject of a `case` expression would
376376
prevent variant inference from working correctly.
377377
([Surya Rose](https://github.com/GearsDatapacks))
378+
379+
- Fixed a bug where the compiler would suggest to use a discarded value defined
380+
in a different function.
381+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

compiler-core/src/analyse.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -560,31 +560,35 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
560560
has_javascript_external: external_javascript.is_some(),
561561
};
562562

563-
let mut typed_arguments = Vec::with_capacity(arguments.len());
564-
for (argument, type_) in arguments.into_iter().zip(&prereg_arguments_types) {
565-
let argument = argument.set_type(type_.clone());
566-
match &argument.names {
567-
ast::ArgNames::Named { .. } | ast::ArgNames::NamedLabelled { .. } => (),
568-
ast::ArgNames::Discard { name, location }
569-
| ast::ArgNames::LabelledDiscard {
570-
name,
571-
name_location: location,
572-
..
573-
} => {
574-
let _ = environment.discarded_names.insert(name.clone(), *location);
575-
}
576-
}
577-
578-
typed_arguments.push(argument);
579-
}
580-
581563
// We have already registered the function in the `register_value_from_function`
582564
// method, but here we must set this as the current function again, so that anything
583565
// we reference in the body of it can be tracked properly in the call graph.
584566
environment.references.set_current_node(name.clone());
585567

568+
let mut typed_arguments = Vec::with_capacity(arguments.len());
569+
586570
// Infer the type using the preregistered args + return types as a starting point
587571
let result = environment.in_new_scope(&mut self.problems, |environment, problems| {
572+
for (argument, type_) in arguments.into_iter().zip(&prereg_arguments_types) {
573+
let argument = argument.set_type(type_.clone());
574+
575+
// We track which arguments are discarded so we can provide nice
576+
// error messages when someone
577+
match &argument.names {
578+
ast::ArgNames::Named { .. } | ast::ArgNames::NamedLabelled { .. } => (),
579+
ast::ArgNames::Discard { name, location }
580+
| ast::ArgNames::LabelledDiscard {
581+
name,
582+
name_location: location,
583+
..
584+
} => {
585+
let _ = environment.discarded_names.insert(name.clone(), *location);
586+
}
587+
}
588+
589+
typed_arguments.push(argument);
590+
}
591+
588592
let mut expr_typer = ExprTyper::new(environment, definition, problems);
589593
expr_typer.hydrator = self
590594
.hydrators

compiler-core/src/type_/tests/warnings.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,3 +4304,13 @@ fn unused_discard_pattern() {
43044304
"
43054305
);
43064306
}
4307+
4308+
#[test]
4309+
fn discarded_argument_suggestion_is_not_given_for_different_functions() {
4310+
assert_no_warnings!(
4311+
"
4312+
pub fn main(_x) { 1 }
4313+
pub fn wibble() { x }
4314+
"
4315+
);
4316+
}

0 commit comments

Comments
 (0)