-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Specialized impl for a type with const generics in another crate causes a stack overflow in rustc #68104
Comments
A bisect shows this stopped crashing after #68118, marked as needing a test |
I was still encountering this in my code with (see https://github.com/akofke/const-generics-stackoverflow-test to try it) // lib.rs
#![feature(const_generics)]
pub struct Num<const N: usize>;
impl<const N: usize> Num<N> {
pub fn foo(&self) {
println!("any N");
}
pub fn new() -> Self {
Self
}
}
// Calling this method in separate crate now works
// (verified that it crashes in earlier nightlies)
impl Num<3> {
pub fn three(&self) {
println!("I'm a 3");
}
}
// Braces around const expression causes crash
impl Num<{4}> {
pub fn four(&self) {
println!("I'm a 4");
}
}
// Another impl this time without the braces is fine
impl Num<4> {
pub fn also_four(&self) {
println!("still a 4");
}
}
const FIVE: usize = 5;
// Using a const even without braces crashes
impl Num<FIVE> {
pub fn five(&self) {
println!("I'm a 5");
}
}
// Using a type alias with braces then an impl on that is fine
pub type Num6 = Num<{6}>;
impl Num6 {
pub fn six(&self) {
println!("Type-aliased 6");
}
} // bin/okay.rs
use mycrate::Num;
use mycrate::Num6;
pub fn main() {
let n = Num::<3>;
n.foo();
n.three();
let n = Num::<1>;
n.foo();
let n = Num::<4>;
n.foo();
n.also_four();
let n = Num::<5>;
n.foo();
let n = Num6::new();
n.six();
} // bin/crash.rs
use mycrate::Num;
pub fn main() {
let n = Num::<4>;
n.four();
} // bin/crash2.rs
use mycrate::Num;
pub fn main() {
let n = Num::<5>;
n.five();
}
Meta
|
Fixes rust-lang#68104 When printing the DefPath for an unevaluated const, we may end up trying to print the same const again. To avoid infinite recursion, we use the 'verbose' format when we try to re-entrantly print a const. This ensures that the pretty-printing process will always terminate.
…r=varkor Properly encode AnonConst into crate metadata Fixes rust-lang#68104 Previous, we were encoding AnonConst as a regular Const, causing us to treat them differently after being deserialized in another compilation session.
…r=varkor Properly encode AnonConst into crate metadata Fixes rust-lang#68104 Previous, we were encoding AnonConst as a regular Const, causing us to treat them differently after being deserialized in another compilation session.
…r=varkor Properly encode AnonConst into crate metadata Fixes rust-lang#68104 Previous, we were encoding AnonConst as a regular Const, causing us to treat them differently after being deserialized in another compilation session.
…r=varkor Properly encode AnonConst into crate metadata Fixes rust-lang#68104 Previous, we were encoding AnonConst as a regular Const, causing us to treat them differently after being deserialized in another compilation session.
Currently with const generics you can create a "specialized" implementation, such as:
Which has the following output:
(playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c5c862b1590029a6c785ec6feddd6da8)
However, when using such a type in another crate this causes a stack overflow in the compiler. For example, with the following code (full example: https://github.com/stbowers/const-generics-test):
I would expect it to produce the same output as the playground link above, however it produces the following:
Meta
The text was updated successfully, but these errors were encountered: