Skip to content

Commit 92512b1

Browse files
fmeasecuviper
authored andcommitted
AST validation: Improve handling of inherent impls nested within functions and anon consts
(cherry picked from commit 7d428db)
1 parent 339fb69 commit 92512b1

File tree

4 files changed

+102
-27
lines changed

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
@@ -925,35 +925,38 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
925925
only_trait: only_trait.then_some(()),
926926
};
927927

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

950-
self.visit_vis(&item.vis);
951-
self.visit_ident(item.ident);
952-
self.with_tilde_const(Some(DisallowTildeConstContext::Impl(item.span)), |this| {
953-
this.visit_generics(generics)
951+
this.visit_vis(&item.vis);
952+
this.visit_ident(item.ident);
953+
this.with_tilde_const(
954+
Some(DisallowTildeConstContext::Impl(item.span)),
955+
|this| this.visit_generics(generics),
956+
);
957+
this.visit_ty(self_ty);
958+
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl);
954959
});
955-
self.visit_ty(self_ty);
956-
walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl);
957960
walk_list!(self, visit_attribute, &item.attrs);
958961
return; // Avoid visiting again.
959962
}
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)