Skip to content

Commit 7d428db

Browse files
committed
AST validation: Improve handling of inherent impls nested within functions and anon consts
1 parent 5257aee commit 7d428db

4 files changed

+102
-27
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -930,35 +930,38 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
930930
only_trait: only_trait.then_some(()),
931931
};
932932

933-
self.visibility_not_permitted(
934-
&item.vis,
935-
errors::VisibilityNotPermittedNote::IndividualImplItems,
936-
);
937-
if let &Unsafe::Yes(span) = unsafety {
938-
self.dcx().emit_err(errors::InherentImplCannotUnsafe {
939-
span: self_ty.span,
940-
annotation_span: span,
941-
annotation: "unsafe",
942-
self_ty: self_ty.span,
943-
});
944-
}
945-
if let &ImplPolarity::Negative(span) = polarity {
946-
self.dcx().emit_err(error(span, "negative", false));
947-
}
948-
if let &Defaultness::Default(def_span) = defaultness {
949-
self.dcx().emit_err(error(def_span, "`default`", true));
950-
}
951-
if let &Const::Yes(span) = constness {
952-
self.dcx().emit_err(error(span, "`const`", true));
953-
}
933+
self.with_in_trait_impl(None, |this| {
934+
this.visibility_not_permitted(
935+
&item.vis,
936+
errors::VisibilityNotPermittedNote::IndividualImplItems,
937+
);
938+
if let &Unsafe::Yes(span) = unsafety {
939+
this.dcx().emit_err(errors::InherentImplCannotUnsafe {
940+
span: self_ty.span,
941+
annotation_span: span,
942+
annotation: "unsafe",
943+
self_ty: self_ty.span,
944+
});
945+
}
946+
if let &ImplPolarity::Negative(span) = polarity {
947+
this.dcx().emit_err(error(span, "negative", false));
948+
}
949+
if let &Defaultness::Default(def_span) = defaultness {
950+
this.dcx().emit_err(error(def_span, "`default`", true));
951+
}
952+
if let &Const::Yes(span) = constness {
953+
this.dcx().emit_err(error(span, "`const`", true));
954+
}
954955

955-
self.visit_vis(&item.vis);
956-
self.visit_ident(item.ident);
957-
self.with_tilde_const(Some(DisallowTildeConstContext::Impl(item.span)), |this| {
958-
this.visit_generics(generics)
956+
this.visit_vis(&item.vis);
957+
this.visit_ident(item.ident);
958+
this.with_tilde_const(
959+
Some(DisallowTildeConstContext::Impl(item.span)),
960+
|this| this.visit_generics(generics),
961+
);
962+
this.visit_ty(self_ty);
963+
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl);
959964
});
960-
self.visit_ty(self_ty);
961-
walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl);
962965
walk_list!(self, visit_attribute, &item.attrs);
963966
return; // Avoid visiting again.
964967
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Regression test for issue #89342 and for part of #119924.
2+
//@ check-pass
3+
4+
struct Expr<const N: u32>;
5+
6+
trait Trait0 {
7+
fn required(_: Expr<{
8+
struct Type;
9+
10+
impl Type {
11+
// This visibility qualifier used to get rejected.
12+
pub fn perform() {}
13+
}
14+
15+
0
16+
}>);
17+
}
18+
19+
trait Trait1 {}
20+
21+
impl Trait1 for ()
22+
where
23+
[(); {
24+
struct Type;
25+
26+
impl Type {
27+
// This visibility qualifier used to get rejected.
28+
pub const STORE: Self = Self;
29+
}
30+
31+
0
32+
}]:
33+
{}
34+
35+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #121607 and for part of issue #119924.
2+
//@ check-pass
3+
4+
trait Trait {
5+
fn provided() {
6+
pub struct Type;
7+
8+
impl Type {
9+
// This visibility qualifier used to get rejected.
10+
pub fn perform() {}
11+
}
12+
}
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression test for part of issue #119924.
2+
//@ check-pass
3+
4+
#![feature(const_trait_impl, effects)]
5+
6+
#[const_trait]
7+
trait Trait {
8+
fn required();
9+
}
10+
11+
impl const Trait for () {
12+
fn required() {
13+
pub struct Type;
14+
15+
impl Type {
16+
// This visibility qualifier used to get rejected.
17+
pub fn perform() {}
18+
}
19+
}
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)