Skip to content

Don't use empty trait names #139568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,21 +602,13 @@ impl<'a> Parser<'a> {
let polarity = self.parse_polarity();

// Parse both types and traits as a type, then reinterpret if necessary.
let err_path = |span| ast::Path::from_ident(Ident::new(kw::Empty, span));
let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
{
let span = self.prev_token.span.between(self.token.span);
self.dcx().emit_err(errors::MissingTraitInTraitImpl {
return Err(self.dcx().create_err(errors::MissingTraitInTraitImpl {
span,
for_span: span.to(self.token.span),
});

P(Ty {
kind: TyKind::Path(None, err_path(span)),
span,
id: DUMMY_NODE_ID,
tokens: None,
})
}));
} else {
self.parse_ty_with_generics_recovery(&generics)?
};
Expand Down Expand Up @@ -657,6 +649,7 @@ impl<'a> Parser<'a> {
other => {
if let TyKind::ImplTrait(_, bounds) = other
&& let [bound] = bounds.as_slice()
&& let GenericBound::Trait(poly_trait_ref) = bound
{
// Suggest removing extra `impl` keyword:
// `impl<T: Default> impl Default for Wrapper<T>`
Expand All @@ -666,12 +659,12 @@ impl<'a> Parser<'a> {
extra_impl_kw,
impl_trait_span: ty_first.span,
});
poly_trait_ref.trait_ref.path.clone()
} else {
self.dcx().emit_err(errors::ExpectedTraitInTraitImplFoundType {
span: ty_first.span,
});
return Err(self.dcx().create_err(
errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
));
}
err_path(ty_first.span)
}
};
let trait_ref = TraitRef { path, ref_id: ty_first.id };
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Option<LexicalScopeBinding<'ra>> {
assert!(ns == TypeNS || ns == ValueNS);
let orig_ident = ident;
if ident.name == kw::Empty {
return Some(LexicalScopeBinding::Res(Res::Err));
}
let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
// FIXME(jseyfried) improve `Self` hygiene
let empty_span = ident.span.with_ctxt(SyntaxContext::root());
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/parser/impl-parsing-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
impl ! {} // OK

default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
//~^ ERROR `default` is not followed by an item
18 changes: 18 additions & 0 deletions tests/ui/parser/impl-parsing-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: `default` is not followed by an item
--> $DIR/impl-parsing-2.rs:3:1
|
LL | default unsafe FAIL
| ^^^^^^^ the `default` qualifier
|
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`

error: expected item, found keyword `unsafe`
--> $DIR/impl-parsing-2.rs:3:9
|
LL | default unsafe FAIL
| ^^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: aborting due to 2 previous errors

5 changes: 0 additions & 5 deletions tests/ui/parser/impl-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ impl ! {} // OK
impl ! where u8: Copy {} // OK

impl Trait Type {} //~ ERROR missing `for` in a trait impl
impl Trait .. {} //~ ERROR missing `for` in a trait impl
impl ?Sized for Type {} //~ ERROR expected a trait, found type
impl ?Sized for .. {} //~ ERROR expected a trait, found type

default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
//~^ ERROR `default` is not followed by an item
37 changes: 2 additions & 35 deletions tests/ui/parser/impl-parsing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,11 @@ help: add `for` here
LL | impl Trait for Type {}
| +++

error: missing `for` in a trait impl
--> $DIR/impl-parsing.rs:5:11
|
LL | impl Trait .. {}
| ^
|
help: add `for` here
|
LL | impl Trait for .. {}
| +++

error: expected a trait, found type
--> $DIR/impl-parsing.rs:6:6
--> $DIR/impl-parsing.rs:5:6
|
LL | impl ?Sized for Type {}
| ^^^^^^

error: expected a trait, found type
--> $DIR/impl-parsing.rs:7:6
|
LL | impl ?Sized for .. {}
| ^^^^^^

error: `default` is not followed by an item
--> $DIR/impl-parsing.rs:9:1
|
LL | default unsafe FAIL
| ^^^^^^^ the `default` qualifier
|
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`

error: expected item, found keyword `unsafe`
--> $DIR/impl-parsing.rs:9:9
|
LL | default unsafe FAIL
| ^^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: aborting due to 6 previous errors
error: aborting due to 2 previous errors

Loading