Skip to content

Commit

Permalink
Improve error messages for generics with default parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
veera-sivarajan committed Feb 21, 2024
1 parent 1d447a9 commit 58fe6db
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
21 changes: 17 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,10 +1252,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
(&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
let did1 = def1.did();
let did2 = def2.did();
let sub_no_defaults_1 =
self.tcx.generics_of(did1).own_args_no_defaults(self.tcx, sub1);
let sub_no_defaults_2 =
self.tcx.generics_of(did2).own_args_no_defaults(self.tcx, sub2);

let generics1 = self.tcx.generics_of(did1);
let generics2 = self.tcx.generics_of(did2);

let non_default_after_default = generics1
.check_concrete_type_after_default(self.tcx, sub1)
|| generics2.check_concrete_type_after_default(self.tcx, sub2);
let sub_no_defaults_1 = if non_default_after_default {
generics1.own_args(sub1)
} else {
generics1.own_args_no_defaults(self.tcx, sub1)
};
let sub_no_defaults_2 = if non_default_after_default {
generics2.own_args(sub2)
} else {
generics2.own_args_no_defaults(self.tcx, sub2)
};
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
let path1 = self.tcx.def_path_str(did1);
let path2 = self.tcx.def_path_str(did2);
Expand Down
27 changes: 27 additions & 0 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,33 @@ impl<'tcx> Generics {
let own = &args[self.parent_count..][..self.params.len()];
if self.has_self && self.parent.is_none() { &own[1..] } else { own }
}

/// Returns true if a concrete type is specified after a default type.
/// For example, consider Box<T, A = Global>
/// Box<usize, CustomAllocator> will return true
/// Box<usize> will return false
pub fn check_concrete_type_after_default(
&'tcx self,
tcx: TyCtxt<'tcx>,
args: &'tcx [ty::GenericArg<'tcx>],
) -> bool {
let mut default_param_seen = false;
for param in self.params.iter() {
if param
.default_value(tcx)
.is_some_and(|default| default.instantiate(tcx, args) == args[param.index as usize])
{
default_param_seen = true;
} else if default_param_seen
&& param.default_value(tcx).is_some_and(|default| {
default.instantiate(tcx, args) != args[param.index as usize]
})
{
return true;
}
}
false
}
}

/// Bounds on generics.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct What<W = usize, X = Vec<W>>(W, X);

fn main() {
let mut b: What<usize> = What(5, vec![1, 2, 3]);
let c: What<usize, String> = What(1, String::from("meow"));
b = c; //~ ERROR mismatched types

let mut e: What<usize> = What(5, vec![1, 2, 3]);
let f: What<usize, Vec<String>> = What(1, vec![String::from("meow")]);
e = f; //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/issue-120785.rs:6:9
|
LL | let mut b: What<usize> = What(5, vec![1, 2, 3]);
| ----------- expected due to this type
LL | let c: What<usize, String> = What(1, String::from("meow"));
LL | b = c;
| ^ expected `What`, found `What<usize, String>`
|
= note: expected struct `What<_, Vec<usize>>`
found struct `What<_, String>`

error[E0308]: mismatched types
--> $DIR/issue-120785.rs:10:9
|
LL | let mut e: What<usize> = What(5, vec![1, 2, 3]);
| ----------- expected due to this type
LL | let f: What<usize, Vec<String>> = What(1, vec![String::from("meow")]);
LL | e = f;
| ^ expected `What`, found `What<usize, Vec<String>>`
|
= note: expected struct `What<_, Vec<usize>>`
found struct `What<_, Vec<String>>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 58fe6db

Please sign in to comment.