Skip to content

Commit 7ae5c7f

Browse files
committed
Avoid an empty trait name in impl blocks.
`resolve_ident_in_lexical_scope` checks for an empty name. Why is this necessary? Because `parse_item_impl` can produce an `impl` block with an empty trait name in some cases. This is pretty gross and very non-obvious. This commit avoids the use of the empty trait name. In one case the trait name is instead pulled from `TyKind::ImplTrait`, which prevents the output for `tests/ui/impl-trait/extra-impl-in-trait-impl.rs` from changing. In the other case we just fail the parse and don't try to recover. I think losing error recovery in this obscure case is worth the code cleanup. This change affects `tests/ui/parser/impl-parsing.rs`, which is split in two, and the obsolete `..` syntax cases are removed (they are tested elsewhere).
1 parent f419b18 commit 7ae5c7f

File tree

6 files changed

+29
-47
lines changed

6 files changed

+29
-47
lines changed

compiler/rustc_parse/src/parser/item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ impl<'a> Parser<'a> {
649649
other => {
650650
if let TyKind::ImplTrait(_, bounds) = other
651651
&& let [bound] = bounds.as_slice()
652+
&& let GenericBound::Trait(poly_trait_ref) = bound
652653
{
653654
// Suggest removing extra `impl` keyword:
654655
// `impl<T: Default> impl Default for Wrapper<T>`
@@ -658,12 +659,12 @@ impl<'a> Parser<'a> {
658659
extra_impl_kw,
659660
impl_trait_span: ty_first.span,
660661
});
662+
poly_trait_ref.trait_ref.path.clone()
661663
} else {
662-
self.dcx().emit_err(errors::ExpectedTraitInTraitImplFoundType {
663-
span: ty_first.span,
664-
});
664+
return Err(self.dcx().create_err(
665+
errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
666+
));
665667
}
666-
ast::Path::from_ident(Ident::new(kw::Empty, ty_first.span))
667668
}
668669
};
669670
let trait_ref = TraitRef { path, ref_id: ty_first.id };

compiler/rustc_resolve/src/ident.rs

-3
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
296296
) -> Option<LexicalScopeBinding<'ra>> {
297297
assert!(ns == TypeNS || ns == ValueNS);
298298
let orig_ident = ident;
299-
if ident.name == kw::Empty {
300-
return Some(LexicalScopeBinding::Res(Res::Err));
301-
}
302299
let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
303300
// FIXME(jseyfried) improve `Self` hygiene
304301
let empty_span = ident.span.with_ctxt(SyntaxContext::root());

tests/ui/parser/impl-parsing-2.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
impl ! {} // OK
2+
3+
default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
4+
//~^ ERROR `default` is not followed by an item

tests/ui/parser/impl-parsing-2.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: `default` is not followed by an item
2+
--> $DIR/impl-parsing-2.rs:3:1
3+
|
4+
LL | default unsafe FAIL
5+
| ^^^^^^^ the `default` qualifier
6+
|
7+
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`
8+
9+
error: expected item, found keyword `unsafe`
10+
--> $DIR/impl-parsing-2.rs:3:9
11+
|
12+
LL | default unsafe FAIL
13+
| ^^^^^^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/parser/impl-parsing.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@ impl ! {} // OK
22
impl ! where u8: Copy {} // OK
33

44
impl Trait Type {} //~ ERROR missing `for` in a trait impl
5-
impl Trait .. {} //~ ERROR missing `for` in a trait impl
65
impl ?Sized for Type {} //~ ERROR expected a trait, found type
7-
impl ?Sized for .. {} //~ ERROR expected a trait, found type
8-
9-
default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
10-
//~^ ERROR `default` is not followed by an item

tests/ui/parser/impl-parsing.stderr

+2-35
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,11 @@ help: add `for` here
99
LL | impl Trait for Type {}
1010
| +++
1111

12-
error: missing `for` in a trait impl
13-
--> $DIR/impl-parsing.rs:5:11
14-
|
15-
LL | impl Trait .. {}
16-
| ^
17-
|
18-
help: add `for` here
19-
|
20-
LL | impl Trait for .. {}
21-
| +++
22-
2312
error: expected a trait, found type
24-
--> $DIR/impl-parsing.rs:6:6
13+
--> $DIR/impl-parsing.rs:5:6
2514
|
2615
LL | impl ?Sized for Type {}
2716
| ^^^^^^
2817

29-
error: expected a trait, found type
30-
--> $DIR/impl-parsing.rs:7:6
31-
|
32-
LL | impl ?Sized for .. {}
33-
| ^^^^^^
34-
35-
error: `default` is not followed by an item
36-
--> $DIR/impl-parsing.rs:9:1
37-
|
38-
LL | default unsafe FAIL
39-
| ^^^^^^^ the `default` qualifier
40-
|
41-
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`
42-
43-
error: expected item, found keyword `unsafe`
44-
--> $DIR/impl-parsing.rs:9:9
45-
|
46-
LL | default unsafe FAIL
47-
| ^^^^^^ expected item
48-
|
49-
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
50-
51-
error: aborting due to 6 previous errors
18+
error: aborting due to 2 previous errors
5219

0 commit comments

Comments
 (0)