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

Recursive type usage in function with Const Generics causes ICE #66205

Closed
felix91gr opened this issue Nov 8, 2019 · 7 comments · Fixed by #67543
Closed

Recursive type usage in function with Const Generics causes ICE #66205

felix91gr opened this issue Nov 8, 2019 · 7 comments · Fixed by #67543
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@felix91gr
Copy link
Contributor

This is a toy example I made in order to later make tests which run for T<const M: usize> with M=0..N.

#![feature(const_generics)]
#[allow(incomplete_features)]

struct Z<const N: i32>;

impl<const N: i32> Z<{N}> {

	fn fact() -> i32 {
		match N {
			k if k < 1 => 1,
			_ => Z::<{N - 1}>::fact()
		}
	}

}

This is the error I get:

error: internal compiler error: src/librustc/ty/subst.rs:651: const parameter `N/#0` (Const { ty: i32, val: Param(N/#0) }/0) out of range when substituting substs=[]

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:890:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Error notes:

  • rustc 1.40.0-nightly (50f8aadd7 2019-11-07) running on x86_64-unknown-linux-gnu

  • compiler flags: -C debuginfo=2 -C incremental --crate-type lib


Questions

I don't know if this kind of pattern is intended as part of the feature or in its future, but if not, I think it can be satisfied by using a procedural macro so that's probably alright.

The point of the pattern, as I mentioned at the beginning of the post, is to be able to test a particular generic type's implementation up to an arbitrary range of numeric constants. While implementing another project to help @varkor test the feature, I stumbled upon the problem of wanting to test hundreds of different constants, but having no way of doing so with say, a for loop over a range of constants. This is one way I thought of making it work.

Am I making the right decisions here? How should I go about testing hundreds of constants? Thanks :)

@felix91gr
Copy link
Contributor Author

felix91gr commented Nov 8, 2019

Note: another example that also panics.

I know, yes, expressions aren't yet supported as const parameters. But it has the same panic, which is WEIRD!

When I wrote this one, I thought "okay maybe inside a match statement, the compiler won't try things like (0 - 1) because the case when N: usize is 0 is already covered... and with exhaustiveness check it should know that N = 0 won't enter the second branch". But I must be wrong there, or otherwise how did it panic?

#![feature(const_generics)]
#[allow(incomplete_features)]

struct Z<const N: usize>;

impl<const N: usize> Z<{N}> {

	fn fact() -> i32 {
		match N {
			0 => 1,
			_ => Z::<{
				match N {
					0 => 0,
					_ => N - 1,
				}
			}>::fact()
		}
	}

}

Error:

error: internal compiler error: src/librustc/ty/subst.rs:651: const parameter `N/#0` (Const { ty: usize, val: Param(N/#0) }/0) out of range when substituting substs=[]

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:890:9

@JohnTitor JohnTitor added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 8, 2019
@hellow554
Copy link
Contributor

hellow554 commented Nov 8, 2019

Minified:

#![feature(const_generics)]

fn fact<const N: usize>() {
    fact::<{ N - 1 }>();
}

I'm pretty sure that I saw this ICE before, but I can't find it ;/

@Centril Centril added F-const_generics `#![feature(const_generics)]` and removed I-nominated labels Nov 8, 2019
@varkor
Copy link
Member

varkor commented Nov 8, 2019

This is just a result of having an expression (N - 1) as a const generic argument. I don't actually think there's a canonical issue, though many of the issues are essentially the same. #60471 is the best tracking issue.

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Nov 12, 2019
@Alexendoo Alexendoo added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 5, 2019
@Alexendoo
Copy link
Member

No longer ICEs on the latest nightly

@felix91gr
Copy link
Contributor Author

Oh, the new output is interesting. It probably needs to be monomorphized so that the compiler detects the infinite recursion. And then we could also write one that doesn't have infinite recursion :3

In order to force an error, a test function should suffice right?

@hellow554
Copy link
Contributor

Oh, the new output is interesting

What do you mean?

@felix91gr
Copy link
Contributor Author

I mean that I imagined it might detect that the function recurses infinitely onto itself. But I guess it reads it lazily, and lets monomorphization do the job of recursing the function? which would make a lot of sense to me.

Centril added a commit to Centril/rust that referenced this issue Dec 23, 2019
…-DPC

Add regression tests for fixed ICEs

Closes rust-lang#61747 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66205 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66270 (fixed by rust-lang#66246)
Closes rust-lang#66868 (fixed by rust-lang#67071)
Closes rust-lang#67424 (fixed by rust-lang#67160)

Also picking a minor nit up from rust-lang#67071 with 101dd7b

r? @Centril
Centril added a commit to Centril/rust that referenced this issue Dec 23, 2019
…-DPC

Add regression tests for fixed ICEs

Closes rust-lang#61747 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66205 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66270 (fixed by rust-lang#66246)
Closes rust-lang#66868 (fixed by rust-lang#67071)
Closes rust-lang#67424 (fixed by rust-lang#67160)

Also picking a minor nit up from rust-lang#67071 with 101dd7b

r? @Centril
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Dec 24, 2019
Add regression tests for fixed ICEs

Closes rust-lang#61747 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66205 (fixed from 1.41.0-nightly (4007d4e 2019-12-01))
Closes rust-lang#66270 (fixed by rust-lang#66246)
Closes rust-lang#67424 (fixed by rust-lang#67160)

Also picking a minor nit up from rust-lang#67071 with 101dd7b

r? @Centril
@bors bors closed this as completed in 07effe1 Dec 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants