Skip to content

Commit 648abd3

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 ef05f9e commit 648abd3

File tree

9 files changed

+112
-67
lines changed

9 files changed

+112
-67
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
@@ -27,7 +27,7 @@ use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list}
2727
use rustc_ast::*;
2828
use rustc_ast_pretty::pprust::{self, State};
2929
use rustc_data_structures::fx::FxIndexMap;
30-
use rustc_errors::{DiagCtxtHandle, E0197};
30+
use rustc_errors::DiagCtxtHandle;
3131
use rustc_feature::Features;
3232
use rustc_parse::validate_attr;
3333
use rustc_session::Session;
@@ -990,49 +990,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
990990
});
991991
}
992992
ItemKind::Impl(box Impl {
993-
safety,
994-
polarity,
995-
defaultness,
996-
constness,
993+
safety: _,
994+
polarity: _,
995+
defaultness: _,
996+
constness: _,
997997
generics,
998998
of_trait: None,
999999
self_ty,
10001000
items,
10011001
}) => {
1002-
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
1003-
span: self_ty.span,
1004-
annotation_span,
1005-
annotation,
1006-
self_ty: self_ty.span,
1007-
only_trait,
1008-
};
1009-
10101002
self.visit_attrs_vis(&item.attrs, &item.vis);
10111003
self.visibility_not_permitted(
10121004
&item.vis,
10131005
errors::VisibilityNotPermittedNote::IndividualImplItems,
10141006
);
1015-
if let &Safety::Unsafe(span) = safety {
1016-
self.dcx()
1017-
.create_err(errors::InherentImplCannot {
1018-
span: self_ty.span,
1019-
annotation_span: span,
1020-
annotation: "unsafe",
1021-
self_ty: self_ty.span,
1022-
only_trait: true,
1023-
})
1024-
.with_code(E0197)
1025-
.emit();
1026-
}
1027-
if let &ImplPolarity::Negative(span) = polarity {
1028-
self.dcx().emit_err(error(span, "negative", false));
1029-
}
1030-
if let &Defaultness::Default(def_span) = defaultness {
1031-
self.dcx().emit_err(error(def_span, "`default`", true));
1032-
}
1033-
if let &Const::Yes(span) = constness {
1034-
self.dcx().emit_err(error(span, "`const`", true));
1035-
}
10361007

10371008
self.with_tilde_const(Some(TildeConstReason::Impl { span: item.span }), |this| {
10381009
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
@@ -463,20 +463,6 @@ pub(crate) struct UnsafeNegativeImpl {
463463
pub r#unsafe: Span,
464464
}
465465

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

compiler/rustc_parse/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@ parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
858858
parse_trait_alias_cannot_be_const = trait aliases cannot be `const`
859859
parse_trait_alias_cannot_be_unsafe = trait aliases cannot be `unsafe`
860860
861+
parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$annotation}
862+
.because = {$annotation} because of this
863+
.type = inherent impl for this type
864+
.only_trait = only trait implementations may be annotated with {$annotation}
865+
861866
parse_transpose_dyn_or_impl = `for<...>` expected after `{$kw}`, not before
862867
.suggestion = move `{$kw}` before the `for<...>`
863868

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
@@ -666,7 +666,41 @@ impl<'a> Parser<'a> {
666666

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