Skip to content

Do not ICE on generic type mismatch in anonymous const #80278

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,10 +1660,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// it's a actual definition. According to the comments (e.g. in
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
// is relied upon by some other code. This might (or might not) need cleanup.
let body_owner_def_id =
let mut body_owner_def_id =
self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
});

if let hir::def::DefKind::AnonConst = self.tcx.hir().def_kind(body_owner_def_id) {
Copy link
Member

Choose a reason for hiding this comment

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

This seems quite subtle, for instance in being sure that AnonConst is the only special case. Are there no general methods for doing this (and should there be, if this pattern is used elsewhere)?

Copy link
Contributor Author

@LeSeulArtichaut LeSeulArtichaut Dec 23, 2020

Choose a reason for hiding this comment

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

IIUC a DefId can be given to any of the variants of DefKind. Maybe closures/generators can cause problems too?

// #80062: we need to pass the containing function to access the generics properly
body_owner_def_id = self
.tcx
.hir()
.get_parent_did(self.tcx.hir().local_def_id_to_hir_id(body_owner_def_id));
}

self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id());

Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/typeck/issue-80062-mismatched-types-anon-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Regression test for issue #80062

fn foo<Foo>() -> Foo { todo!() }

fn bar<T>() {
let _: [u8; foo::<T>()];
//~^ ERROR the size for values of type `T` cannot be known at compilation time
//~^^ ERROR mismatched types
//~^^^ ERROR the size for values of type `T` cannot be known at compilation time
}

fn main() {}
41 changes: 41 additions & 0 deletions src/test/ui/typeck/issue-80062-mismatched-types-anon-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:23
|
LL | fn foo<Foo>() -> Foo { todo!() }
| --- required by this bound in `foo`
LL |
LL | fn bar<T>() {
| - this type parameter needs to be `Sized`
LL | let _: [u8; foo::<T>()];
| ^ doesn't have a size known at compile-time
|
help: consider relaxing the implicit `Sized` restriction
|
LL | fn foo<Foo: ?Sized>() -> Foo { todo!() }
| ^^^^^^^^

error[E0308]: mismatched types
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17
|
LL | fn bar<T>() {
| - this type parameter
LL | let _: [u8; foo::<T>()];
| ^^^^^^^^^^ expected `usize`, found type parameter `T`
|
= note: expected type `usize`
found type parameter `T`

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17
|
LL | fn bar<T>() {
| - this type parameter needs to be `Sized`
LL | let _: [u8; foo::<T>()];
| ^^^^^^^^ doesn't have a size known at compile-time
|
= note: the return type of a function must have a statically known size

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.