Skip to content

Commit fa73390

Browse files
committed
Move trait impl modifier errors to parsing
This is a technically a breaking change for what can be parsed in `#[cfg(false)]`.
1 parent 39c5d6d commit fa73390

File tree

10 files changed

+123
-78
lines changed

10 files changed

+123
-78
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ ast_passes_generic_default_trailing = generic parameters with a default must be
175175
ast_passes_incompatible_features = `{$f1}` and `{$f2}` are incompatible, using them at the same time is not allowed
176176
.help = remove one of these features
177177
178-
ast_passes_inherent_cannot_be = inherent impls cannot be {$annotation}
179-
.because = {$annotation} because of this
180-
.type = inherent impl for this type
181-
.only_trait = only trait implementations may be annotated with {$annotation}
182-
183178
ast_passes_item_invalid_safety = items outside of `unsafe extern {"{ }"}` cannot be declared with `safe` safety qualifier
184179
.suggestion = remove safe from this item
185180

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list}
2626
use rustc_ast::*;
2727
use rustc_ast_pretty::pprust::{self, State};
2828
use rustc_data_structures::fx::FxIndexMap;
29-
use rustc_errors::{DiagCtxtHandle, E0197};
29+
use rustc_errors::DiagCtxtHandle;
3030
use rustc_feature::Features;
3131
use rustc_parse::validate_attr;
3232
use rustc_session::Session;
@@ -993,49 +993,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
993993
});
994994
}
995995
ItemKind::Impl(box Impl {
996-
safety,
997-
polarity,
998-
defaultness,
999-
constness,
996+
safety: _,
997+
polarity: _,
998+
defaultness: _,
999+
constness: _,
10001000
generics,
10011001
of_trait: None,
10021002
self_ty,
10031003
items,
10041004
}) => {
1005-
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
1006-
span: self_ty.span,
1007-
annotation_span,
1008-
annotation,
1009-
self_ty: self_ty.span,
1010-
only_trait,
1011-
};
1012-
10131005
self.visit_attrs_vis(&item.attrs, &item.vis);
10141006
self.visibility_not_permitted(
10151007
&item.vis,
10161008
errors::VisibilityNotPermittedNote::IndividualImplItems,
10171009
);
1018-
if let &Safety::Unsafe(span) = safety {
1019-
self.dcx()
1020-
.create_err(errors::InherentImplCannot {
1021-
span: self_ty.span,
1022-
annotation_span: span,
1023-
annotation: "unsafe",
1024-
self_ty: self_ty.span,
1025-
only_trait: true,
1026-
})
1027-
.with_code(E0197)
1028-
.emit();
1029-
}
1030-
if let &ImplPolarity::Negative(span) = polarity {
1031-
self.dcx().emit_err(error(span, "negative", false));
1032-
}
1033-
if let &Defaultness::Default(def_span) = defaultness {
1034-
self.dcx().emit_err(error(def_span, "`default`", true));
1035-
}
1036-
if let &Const::Yes(span) = constness {
1037-
self.dcx().emit_err(error(span, "`const`", true));
1038-
}
10391010

10401011
self.with_tilde_const(Some(TildeConstReason::Impl { span: item.span }), |this| {
10411012
this.visit_generics(generics)

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -464,20 +464,6 @@ pub(crate) struct UnsafeNegativeImpl {
464464
pub r#unsafe: Span,
465465
}
466466

467-
#[derive(Diagnostic)]
468-
#[diag(ast_passes_inherent_cannot_be)]
469-
pub(crate) struct InherentImplCannot<'a> {
470-
#[primary_span]
471-
pub span: Span,
472-
#[label(ast_passes_because)]
473-
pub annotation_span: Span,
474-
pub annotation: &'a str,
475-
#[label(ast_passes_type)]
476-
pub self_ty: Span,
477-
#[note(ast_passes_only_trait)]
478-
pub only_trait: bool,
479-
}
480-
481467
#[derive(Diagnostic)]
482468
#[diag(ast_passes_unsafe_item)]
483469
pub(crate) struct UnsafeItem {

compiler/rustc_parse/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,11 @@ parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
869869
parse_trait_alias_cannot_be_const = trait aliases cannot be `const`
870870
parse_trait_alias_cannot_be_unsafe = trait aliases cannot be `unsafe`
871871
872+
parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$annotation}
873+
.because = {$annotation} because of this
874+
.type = inherent impl for this type
875+
.only_trait = only trait implementations may be annotated with {$annotation}
876+
872877
parse_transpose_dyn_or_impl = `for<...>` expected after `{$kw}`, not before
873878
.suggestion = move `{$kw}` before the `for<...>`
874879

compiler/rustc_parse/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ pub(crate) struct BadQPathStage2 {
7171
pub wrap: WrapType,
7272
}
7373

74+
#[derive(Diagnostic)]
75+
#[diag(parse_trait_impl_modifier_in_inherent_impl)]
76+
pub(crate) struct TraitImplModifierInInherentImpl<'a> {
77+
#[primary_span]
78+
pub span: Span,
79+
#[label(parse_because)]
80+
pub annotation_span: Span,
81+
pub annotation: &'a str,
82+
#[label(parse_type)]
83+
pub self_ty: Span,
84+
#[note(parse_only_trait)]
85+
pub only_trait: bool,
86+
}
87+
7488
#[derive(Subdiagnostic)]
7589
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
7690
pub(crate) struct WrapType {

compiler/rustc_parse/src/parser/item.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,41 @@ impl<'a> Parser<'a> {
665665

666666
(Some(trait_ref), ty_second)
667667
}
668-
None => (None, ty_first), // impl Type
668+
None => {
669+
let self_ty = ty_first;
670+
let error = |annotation_span, annotation, only_trait| {
671+
errors::TraitImplModifierInInherentImpl {
672+
span: self_ty.span,
673+
annotation_span,
674+
annotation,
675+
self_ty: self_ty.span,
676+
only_trait,
677+
}
678+
};
679+
680+
if let Safety::Unsafe(span) = safety {
681+
self.dcx()
682+
.create_err(errors::TraitImplModifierInInherentImpl {
683+
span: self_ty.span,
684+
annotation_span: span,
685+
annotation: "unsafe",
686+
self_ty: self_ty.span,
687+
only_trait: true,
688+
})
689+
.with_code(E0197)
690+
.emit();
691+
}
692+
if let ImplPolarity::Negative(span) = polarity {
693+
self.dcx().emit_err(error(span, "negative", false));
694+
}
695+
if let Defaultness::Default(def_span) = defaultness {
696+
self.dcx().emit_err(error(def_span, "`default`", true));
697+
}
698+
if let Const::Yes(span) = constness {
699+
self.dcx().emit_err(error(span, "`const`", true));
700+
}
701+
(None, self_ty)
702+
}
669703
};
670704
Ok(ItemKind::Impl(Box::new(Impl {
671705
safety,

tests/ui/parser/default-on-wrong-item-kind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod free_items {
1919
default union foo {} //~ ERROR a union cannot be `default`
2020
default trait foo {} //~ ERROR a trait cannot be `default`
2121
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
22-
default impl foo {}
22+
default impl foo {} //~ ERROR inherent impls cannot be `default`
2323
default!();
2424
default::foo::bar!();
2525
default default!(); //~ ERROR an item macro invocation cannot be `default`
@@ -53,7 +53,7 @@ extern "C" {
5353
//~^ ERROR trait is not supported in `extern` blocks
5454
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
5555
//~^ ERROR trait alias is not supported in `extern` blocks
56-
default impl foo {}
56+
default impl foo {} //~ ERROR inherent impls cannot be `default`
5757
//~^ ERROR implementation is not supported in `extern` blocks
5858
default!();
5959
default::foo::bar!();
@@ -90,7 +90,7 @@ impl S {
9090
//~^ ERROR trait is not supported in `trait`s or `impl`s
9191
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
9292
//~^ ERROR trait alias is not supported in `trait`s or `impl`s
93-
default impl foo {}
93+
default impl foo {} //~ ERROR inherent impls cannot be `default`
9494
//~^ ERROR implementation is not supported in `trait`s or `impl`s
9595
default!();
9696
default::foo::bar!();
@@ -127,7 +127,7 @@ trait T {
127127
//~^ ERROR trait is not supported in `trait`s or `impl`s
128128
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
129129
//~^ ERROR trait alias is not supported in `trait`s or `impl`s
130-
default impl foo {}
130+
default impl foo {} //~ ERROR inherent impls cannot be `default`
131131
//~^ ERROR implementation is not supported in `trait`s or `impl`s
132132
default!();
133133
default::foo::bar!();

tests/ui/parser/default-on-wrong-item-kind.stderr

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ LL | default trait foo = Ord;
7878
|
7979
= note: only associated `fn`, `const`, and `type` items can be `default`
8080

81+
error: inherent impls cannot be `default`
82+
--> $DIR/default-on-wrong-item-kind.rs:22:18
83+
|
84+
LL | default impl foo {}
85+
| ------- ^^^ inherent impl for this type
86+
| |
87+
| `default` because of this
88+
|
89+
= note: only trait implementations may be annotated with `default`
90+
8191
error: an item macro invocation cannot be `default`
8292
--> $DIR/default-on-wrong-item-kind.rs:25:5
8393
|
@@ -275,6 +285,16 @@ LL | default trait foo = Ord;
275285
|
276286
= help: consider moving the trait alias out to a nearby module scope
277287

288+
error: inherent impls cannot be `default`
289+
--> $DIR/default-on-wrong-item-kind.rs:56:18
290+
|
291+
LL | default impl foo {}
292+
| ------- ^^^ inherent impl for this type
293+
| |
294+
| `default` because of this
295+
|
296+
= note: only trait implementations may be annotated with `default`
297+
278298
error: implementation is not supported in `extern` blocks
279299
--> $DIR/default-on-wrong-item-kind.rs:56:5
280300
|
@@ -489,6 +509,16 @@ LL | default trait foo = Ord;
489509
|
490510
= help: consider moving the trait alias out to a nearby module scope
491511

512+
error: inherent impls cannot be `default`
513+
--> $DIR/default-on-wrong-item-kind.rs:93:18
514+
|
515+
LL | default impl foo {}
516+
| ------- ^^^ inherent impl for this type
517+
| |
518+
| `default` because of this
519+
|
520+
= note: only trait implementations may be annotated with `default`
521+
492522
error: implementation is not supported in `trait`s or `impl`s
493523
--> $DIR/default-on-wrong-item-kind.rs:93:5
494524
|
@@ -703,6 +733,16 @@ LL | default trait foo = Ord;
703733
|
704734
= help: consider moving the trait alias out to a nearby module scope
705735

736+
error: inherent impls cannot be `default`
737+
--> $DIR/default-on-wrong-item-kind.rs:130:18
738+
|
739+
LL | default impl foo {}
740+
| ------- ^^^ inherent impl for this type
741+
| |
742+
| `default` because of this
743+
|
744+
= note: only trait implementations may be annotated with `default`
745+
706746
error: implementation is not supported in `trait`s or `impl`s
707747
--> $DIR/default-on-wrong-item-kind.rs:130:5
708748
|
@@ -759,5 +799,5 @@ LL | default macro_rules! foo {}
759799
|
760800
= help: consider moving the macro definition out to a nearby module scope
761801

762-
error: aborting due to 95 previous errors
802+
error: aborting due to 99 previous errors
763803

tests/ui/traits/const-traits/macro-const-trait-bound-theoretical-regression.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
error: macro expansion ignores keyword `dyn` and any tokens following
2-
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:14:31
3-
|
4-
LL | (dyn $c:ident Trait) => { dyn $c Trait {} };
5-
| ^^^
6-
...
7-
LL | demo! { dyn const Trait }
8-
| ------------------------- caused by the macro expansion here
9-
|
10-
= note: the usage of `demo!` is likely invalid in item context
11-
121
error: inherent impls cannot be `const`
132
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:10:40
143
|
@@ -24,6 +13,17 @@ LL | demo! { impl const Trait }
2413
= note: only trait implementations may be annotated with `const`
2514
= note: this error originates in the macro `demo` (in Nightly builds, run with -Z macro-backtrace for more info)
2615

16+
error: macro expansion ignores keyword `dyn` and any tokens following
17+
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:14:31
18+
|
19+
LL | (dyn $c:ident Trait) => { dyn $c Trait {} };
20+
| ^^^
21+
...
22+
LL | demo! { dyn const Trait }
23+
| ------------------------- caused by the macro expansion here
24+
|
25+
= note: the usage of `demo!` is likely invalid in item context
26+
2727
error[E0658]: const trait impls are experimental
2828
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:17:14
2929
|

tests/ui/traits/syntax-trait-polarity.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | impl !TestType {}
66
| |
77
| negative because of this
88

9+
error: inherent impls cannot be negative
10+
--> $DIR/syntax-trait-polarity.rs:18:10
11+
|
12+
LL | impl<T> !TestType2<T> {}
13+
| -^^^^^^^^^^^^ inherent impl for this type
14+
| |
15+
| negative because of this
16+
917
error[E0198]: negative impls cannot be unsafe
1018
--> $DIR/syntax-trait-polarity.rs:12:13
1119
|
@@ -15,14 +23,6 @@ LL | unsafe impl !Send for TestType {}
1523
| | negative because of this
1624
| unsafe because of this
1725

18-
error: inherent impls cannot be negative
19-
--> $DIR/syntax-trait-polarity.rs:18:10
20-
|
21-
LL | impl<T> !TestType2<T> {}
22-
| -^^^^^^^^^^^^ inherent impl for this type
23-
| |
24-
| negative because of this
25-
2626
error[E0198]: negative impls cannot be unsafe
2727
--> $DIR/syntax-trait-polarity.rs:21:16
2828
|

0 commit comments

Comments
 (0)