rust icon indicating copy to clipboard operation
rust copied to clipboard

AST validation doesn't correctly deal with impls nested within associated functions

Open fmease opened this issue 1 year ago • 2 comments

The following programs get wrongfully rejected since my PR #119505 (nightly-2024-01-04):

pub struct S;

trait Trait {
    fn provided() {
        impl S {
            pub fn perform() {} //~ ERROR visibility qualifiers are not permitted here
        }
    }
}
pub struct S;
struct Expr<const N: u32>;

trait Trait {
    fn required(_: Expr<{
        impl S {
            pub fn perform() {} //~ ERROR visibility qualifiers are not permitted here
        }
        0
    }>);
}

The following program was incorrectly rejected even before my PR #119505 (nightly-2024-01-04):

#![feature(const_trait_impl, effects)]

pub struct S;

#[const_trait]
trait Trait {
    fn required();
}

impl const Trait for () {
    fn required() {
        impl S {
            pub fn perform() {} //~ ERROR visibility qualifiers are not permitted here
        }
    }
}

The following programs lead to an ICE even before my PR (e.g., in nightly-2023-12-31):

#![feature(const_trait_impl, effects)]

pub struct S;
#[const_trait]
trait Trait {
    fn provided() {
        impl S {
            fn perform<T: ~const Trait>() {} // should've gotten rejected during AST validation
            //~^ ICE no host param id for call in const yet no errors reported
        }
    }
}
#![feature(const_trait_impl, effects)]

pub struct S;
struct Expr<const N: u32>;

#[const_trait]
trait Trait {
    fn required(_: Expr<{
        impl S {
            fn perform<T: ~const Trait>() {} // should've gotten rejected during AST validation
            //~^ ICE no host param id for call in const yet no errors reported
        }
	0
    }>);
}
#![feature(const_trait_impl, effects)]

struct S;
#[const_trait]
trait Trait<const N: u32> {}

const fn f<T: Trait<{
    struct I<U: ~const Trait<0>>(U); // should've gotten rejected during AST validation
    //~^ ICE no host param id for call in const yet no errors reported
    0
}>>() {}

There are many more issues and probably many more ways to reproduce this, e.g. we don't visit attributes on associated functions under certain circumstances (I couldn't find a reproducer yet in which you can observe the bug).

fmease avatar Jan 13 '24 12:01 fmease

Ah nice, I found another one reported by someone else ages ago while triaging needs-triage-legacy issues: #89342.

fmease avatar Jan 23 '24 23:01 fmease

Lol, no code path properly accounts for AnonConst in AstValidator:

fn f(_: impl Trait<{
    fn g(_: impl Sized) {} //~ ERROR nested `impl Trait` is not allowed
    false
}>) {}
trait Trait<const B: bool> {}

fmease avatar Feb 02 '24 15:02 fmease

Fun, the following also gets rejected (not strictly AST validation but AST passes, namely feature_gate):

struct Const<const N: u32>;

type T = Const<{
    fn take(_: impl Sized) {}
    0
}>;
error[E0658]: `impl Trait` in type aliases is unstable
 --> src/lib.rs:4:16
  |
4 |     fn take(_: impl Sized) {}
  |                ^^^^^^^^^^
  |
  = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
  = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable
  = note: this compiler was built on 2024-02-01; consider upgrading it if it is out of date

fmease avatar Feb 06 '24 02:02 fmease