Skip to content

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

@fmease

Description

@fmease
Member

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).

Activity

added
A-associated-itemsArea: Associated items (types, constants & functions)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
C-bugCategory: This is a bug.
on Jan 13, 2024
self-assigned this
on Jan 13, 2024
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jan 13, 2024
removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jan 13, 2024
added a commit that references this issue on Jan 18, 2024
fmease

fmease commented on Jan 23, 2024

@fmease
MemberAuthor

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

fmease

fmease commented on Feb 2, 2024

@fmease
MemberAuthor

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

fmease commented on Feb 6, 2024

@fmease
MemberAuthor

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
added a commit that references this issue on Mar 7, 2024
2e3bde2
added a commit that references this issue on Mar 8, 2024
added
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
on Apr 5, 2024
added
S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.
on Apr 15, 2024
removed
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.
on Mar 28, 2025
added a commit that references this issue on Mar 28, 2025
52aed95
added a commit that references this issue on Mar 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @matthiaskrgr@fmease@rustbot

    Issue actions

      AST validation doesn't correctly deal with impls nested within associated functions · Issue #119924 · rust-lang/rust