Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: avoid mismatch between variance index and hir generic #116045

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,20 +1755,41 @@ fn check_variances_for_type_defn<'tcx>(
.collect::<FxHashSet<_>>()
});

let ty_generics = tcx.generics_of(item.owner_id);

for (index, _) in variances.iter().enumerate() {
let parameter = Parameter(index as u32);

if constrained_parameters.contains(&parameter) {
continue;
}

let param = &hir_generics.params[index];
let ty_param = &ty_generics.params[index];
let mut hir_param = &hir_generics.params[index];

if ty_param.name != hir_param.name.ident().name {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ty_param.name != hir_param.name.ident().name {
if ty_param.def_id != hir_param.def_id {

Working with def_ids is always more robust.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

// valid programs always have lifetimes before types in the generic parameter list
// ty_generics are normalized to be in this required order, and variances are built
// from ty generics, not from hir generics. but we need hir generics to get
// a span out
//
// if they aren't in the same order, then the user has written invalid code, and already
// got an error about it (or I'm wrong about this)
tcx.sess
.delay_span_bug(hir_param.span, "hir generics and ty generics in different order");
for hp in hir_generics.params {
if hp.name.ident().name == ty_param.name {
hir_param = hp;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just silence the error? The program is invalid anyway, and we check that by delaying a bug.

Suggested change
for hp in hir_generics.params {
if hp.name.ident().name == ty_param.name {
hir_param = hp;
break;
}
}
continue;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems reasonable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for hp in hir_generics.params {
if hp.name.ident().name == ty_param.name {
hir_param = hp;
break;
}
}
if let Some(node) = tcx.find_node_by_def_id(ty_param.def_id) {
hir_param = node.expect_generic_param();
}

}

match param.name {
match hir_param.name {
hir::ParamName::Error => {}
_ => {
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
report_bivariance(tcx, param, has_explicit_bounds);
report_bivariance(tcx, hir_param, has_explicit_bounds);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/generics/issue-83556.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct Foo<T, 'a>(&'a ());
//~^ ERROR lifetime parameters must be declared prior to
//~| ERROR parameter `T` is never used

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/generics/issue-83556.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: lifetime parameters must be declared prior to type and const parameters
--> $DIR/issue-83556.rs:1:15
|
LL | struct Foo<T, 'a>(&'a ());
| ----^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, T>`

error[E0392]: parameter `T` is never used
--> $DIR/issue-83556.rs:1:12
|
LL | struct Foo<T, 'a>(&'a ());
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: usize` instead

error: aborting due to 2 previous errors

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