Skip to content

Commit ffc9e83

Browse files
Improve error messages for generics with default parameters
Fixes #120785
1 parent 1d447a9 commit ffc9e83

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
12521252
(&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
12531253
let did1 = def1.did();
12541254
let did2 = def2.did();
1255-
let sub_no_defaults_1 =
1256-
self.tcx.generics_of(did1).own_args_no_defaults(self.tcx, sub1);
1257-
let sub_no_defaults_2 =
1258-
self.tcx.generics_of(did2).own_args_no_defaults(self.tcx, sub2);
1255+
1256+
let generics1 = self.tcx.generics_of(did1);
1257+
let generics2 = self.tcx.generics_of(did2);
1258+
1259+
let non_default_after_default = generics1
1260+
.check_concrete_type_after_default(self.tcx, sub1)
1261+
|| generics2.check_concrete_type_after_default(self.tcx, sub2);
1262+
let sub_no_defaults_1 = if non_default_after_default {
1263+
generics1.own_args(sub1)
1264+
} else {
1265+
generics1.own_args_no_defaults(self.tcx, sub1)
1266+
};
1267+
let sub_no_defaults_2 = if non_default_after_default {
1268+
generics2.own_args(sub2)
1269+
} else {
1270+
generics2.own_args_no_defaults(self.tcx, sub2)
1271+
};
12591272
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
12601273
let path1 = self.tcx.def_path_str(did1);
12611274
let path2 = self.tcx.def_path_str(did2);

compiler/rustc_middle/src/ty/generics.rs

+27
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,33 @@ impl<'tcx> Generics {
360360
let own = &args[self.parent_count..][..self.params.len()];
361361
if self.has_self && self.parent.is_none() { &own[1..] } else { own }
362362
}
363+
364+
/// Returns true if a concrete type is specified after a default type.
365+
/// For example, consider `struct T<W = usize, X = Vec<W>>(W, X)`
366+
/// `T<usize, String>` will return true
367+
/// `T<usize>` will return false
368+
pub fn check_concrete_type_after_default(
369+
&'tcx self,
370+
tcx: TyCtxt<'tcx>,
371+
args: &'tcx [ty::GenericArg<'tcx>],
372+
) -> bool {
373+
let mut default_param_seen = false;
374+
for param in self.params.iter() {
375+
if param
376+
.default_value(tcx)
377+
.is_some_and(|default| default.instantiate(tcx, args) == args[param.index as usize])
378+
{
379+
default_param_seen = true;
380+
} else if default_param_seen
381+
&& param.default_value(tcx).is_some_and(|default| {
382+
default.instantiate(tcx, args) != args[param.index as usize]
383+
})
384+
{
385+
return true;
386+
}
387+
}
388+
false
389+
}
363390
}
364391

365392
/// Bounds on generics.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct What<W = usize, X = Vec<W>>(W, X);
2+
3+
fn main() {
4+
let mut b: What<usize> = What(5, vec![1, 2, 3]);
5+
let c: What<usize, String> = What(1, String::from("meow"));
6+
b = c; //~ ERROR mismatched types
7+
8+
let mut e: What<usize> = What(5, vec![1, 2, 3]);
9+
let f: What<usize, Vec<String>> = What(1, vec![String::from("meow")]);
10+
e = f; //~ ERROR mismatched types
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/clarify-error-for-generics-with-default-issue-120785.rs:6:9
3+
|
4+
LL | let mut b: What<usize> = What(5, vec![1, 2, 3]);
5+
| ----------- expected due to this type
6+
LL | let c: What<usize, String> = What(1, String::from("meow"));
7+
LL | b = c;
8+
| ^ expected `What`, found `What<usize, String>`
9+
|
10+
= note: expected struct `What<_, Vec<usize>>`
11+
found struct `What<_, String>`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/clarify-error-for-generics-with-default-issue-120785.rs:10:9
15+
|
16+
LL | let mut e: What<usize> = What(5, vec![1, 2, 3]);
17+
| ----------- expected due to this type
18+
LL | let f: What<usize, Vec<String>> = What(1, vec![String::from("meow")]);
19+
LL | e = f;
20+
| ^ expected `What`, found `What<usize, Vec<String>>`
21+
|
22+
= note: expected struct `What<_, Vec<usize>>`
23+
found struct `What<_, Vec<String>>`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)