Skip to content

Commit 9a4eac3

Browse files
committedFeb 5, 2020
ast_validation: fix visiting bug.
1 parent 67c29ed commit 9a4eac3

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed
 

‎src/librustc_ast_passes/ast_validation.rs

+28-21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ struct AstValidator<'a> {
8181
}
8282

8383
impl<'a> AstValidator<'a> {
84+
fn with_in_trait_impl(&mut self, is_in: bool, f: impl FnOnce(&mut Self)) {
85+
let old = mem::replace(&mut self.in_trait_impl, is_in);
86+
f(self);
87+
self.in_trait_impl = old;
88+
}
89+
8490
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
8591
let old = mem::replace(&mut self.is_impl_trait_banned, true);
8692
f(self);
@@ -737,28 +743,29 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
737743
ref self_ty,
738744
items: _,
739745
} => {
740-
let old_in_trait_impl = mem::replace(&mut self.in_trait_impl, true);
741-
742-
self.invalid_visibility(&item.vis, None);
743-
if let TyKind::Err = self_ty.kind {
744-
self.err_handler()
745-
.struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
746-
.help("use `auto trait Trait {}` instead")
746+
self.with_in_trait_impl(true, |this| {
747+
this.invalid_visibility(&item.vis, None);
748+
if let TyKind::Err = self_ty.kind {
749+
this.err_handler()
750+
.struct_span_err(
751+
item.span,
752+
"`impl Trait for .. {}` is an obsolete syntax",
753+
)
754+
.help("use `auto trait Trait {}` instead")
755+
.emit();
756+
}
757+
if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
758+
struct_span_err!(
759+
this.session,
760+
item.span,
761+
E0198,
762+
"negative impls cannot be unsafe"
763+
)
747764
.emit();
748-
}
749-
if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
750-
struct_span_err!(
751-
self.session,
752-
item.span,
753-
E0198,
754-
"negative impls cannot be unsafe"
755-
)
756-
.emit();
757-
}
758-
759-
visit::walk_item(self, item);
765+
}
760766

761-
self.in_trait_impl = old_in_trait_impl;
767+
visit::walk_item(this, item);
768+
});
762769
return; // Avoid visiting again.
763770
}
764771
ItemKind::Impl {
@@ -1142,7 +1149,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11421149
}
11431150
}
11441151

1145-
visit::walk_assoc_item(self, item, ctxt);
1152+
self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));
11461153
}
11471154
}
11481155

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Make sure we don't propagate restrictions on trait impl items to items inside them.
2+
3+
// check-pass
4+
// edition:2018
5+
6+
fn main() {}
7+
8+
trait X {
9+
fn foo();
10+
}
11+
12+
impl X for () {
13+
fn foo() {
14+
struct S;
15+
impl S {
16+
pub const X: u8 = 0;
17+
pub const fn bar() {}
18+
async fn qux() {}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.