Skip to content

Do not ICE on incorrect const generic #60281

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
wants to merge 1 commit into from
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
5 changes: 4 additions & 1 deletion src/librustc/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
let opt_ty = self.substs.get(p.idx as usize).map(|k| k.unpack());
let ty = match opt_ty {
Some(UnpackedKind::Type(ty)) => ty,
Some(UnpackedKind::Const(ty::Const { ty, ..})) => ty,
None => source_ty,
Copy link
Contributor Author

@estebank estebank Apr 25, 2019

Choose a reason for hiding this comment

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

This is incredibly suspect, but it is the branch that does fail (I think after adding the one above).

We could replace it with a delay_span_bug instead.

Copy link
Member

Choose a reason for hiding this comment

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

I think this hides the issue without actually fixing it. What we want to do is forbid const parameters to have type parameters as their types. These used to be forbidden, but it hasn't been merged yet.

_ => {
let span = self.span.unwrap_or(DUMMY_SP);
span_bug!(
Expand All @@ -551,7 +553,8 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
source_ty,
p.idx,
self.root_ty,
self.substs);
self.substs,
);
}
};

Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issue-60264.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::marker::PhantomData;

struct B<T = N, const N: T>(PhantomData<[T; N]>);
//~^ ERROR expected type, found const parameter `N`
//~| ERROR const generics are unstable
//~| ERROR mismatched types

fn main() {}
28 changes: 28 additions & 0 deletions src/test/ui/const-generics/issue-60264.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0573]: expected type, found const parameter `N`
--> $DIR/issue-60264.rs:3:14
|
LL | struct B<T = N, const N: T>(PhantomData<[T; N]>);
| ^ help: a struct with a similar name exists: `B`

error[E0658]: const generics are unstable
--> $DIR/issue-60264.rs:3:23
|
LL | struct B<T = N, const N: T>(PhantomData<[T; N]>);
| ^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
= help: add #![feature(const_generics)] to the crate attributes to enable

error[E0308]: mismatched types
--> $DIR/issue-60264.rs:3:45
|
LL | struct B<T = N, const N: T>(PhantomData<[T; N]>);
| ^ expected usize, found type parameter
|
= note: expected type `usize`
found type `T`

error: aborting due to 3 previous errors

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