Skip to content

Commit 91525fd

Browse files
committed
Tweak output for invalid negative impl AST errors
1 parent 2b0cfa5 commit 91525fd

File tree

18 files changed

+56
-52
lines changed

18 files changed

+56
-52
lines changed

src/librustc_ast/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,14 +2117,14 @@ pub enum ImplPolarity {
21172117
/// `impl Trait for Type`
21182118
Positive,
21192119
/// `impl !Trait for Type`
2120-
Negative,
2120+
Negative(Span),
21212121
}
21222122

21232123
impl fmt::Debug for ImplPolarity {
21242124
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21252125
match *self {
21262126
ImplPolarity::Positive => "positive".fmt(f),
2127-
ImplPolarity::Negative => "negative".fmt(f),
2127+
ImplPolarity::Negative(_) => "negative".fmt(f),
21282128
}
21292129
}
21302130
}

src/librustc_ast_passes/ast_validation.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
779779
defaultness: _,
780780
constness: _,
781781
generics: _,
782-
of_trait: Some(_),
782+
of_trait: Some(ref t),
783783
ref self_ty,
784784
items: _,
785785
} => {
@@ -794,10 +794,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
794794
.help("use `auto trait Trait {}` instead")
795795
.emit();
796796
}
797-
if let (Unsafe::Yes(span), ImplPolarity::Negative) = (unsafety, polarity) {
797+
if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) {
798798
struct_span_err!(
799799
this.session,
800-
item.span,
800+
sp.to(t.path.span),
801801
E0198,
802802
"negative impls cannot be unsafe"
803803
)
@@ -816,7 +816,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
816816
constness,
817817
generics: _,
818818
of_trait: None,
819-
self_ty: _,
819+
ref self_ty,
820820
items: _,
821821
} => {
822822
self.invalid_visibility(
@@ -826,28 +826,36 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
826826
if let Unsafe::Yes(span) = unsafety {
827827
struct_span_err!(
828828
self.session,
829-
item.span,
829+
vec![span, self_ty.span],
830830
E0197,
831831
"inherent impls cannot be unsafe"
832832
)
833833
.span_label(span, "unsafe because of this")
834+
.span_label(self_ty.span, "inherent impl for this type")
834835
.emit();
835836
}
836-
if polarity == ImplPolarity::Negative {
837-
self.err_handler().span_err(item.span, "inherent impls cannot be negative");
837+
if let ImplPolarity::Negative(span) = polarity {
838+
self.err_handler().span_err(span, "inherent impls cannot be negative");
838839
}
839840
if let Defaultness::Default(def_span) = defaultness {
840-
let span = self.session.source_map().def_span(item.span);
841841
self.err_handler()
842-
.struct_span_err(span, "inherent impls cannot be `default`")
842+
.struct_span_err(
843+
vec![def_span, self_ty.span],
844+
"inherent impls cannot be `default`",
845+
)
843846
.span_label(def_span, "`default` because of this")
847+
.span_label(self_ty.span, "inherent impl for this type")
844848
.note("only trait implementations may be annotated with `default`")
845849
.emit();
846850
}
847851
if let Const::Yes(span) = constness {
848852
self.err_handler()
849-
.struct_span_err(item.span, "inherent impls cannot be `const`")
853+
.struct_span_err(
854+
vec![span, self_ty.span],
855+
"inherent impls cannot be `const`",
856+
)
850857
.span_label(span, "`const` because of this")
858+
.span_label(self_ty.span, "inherent impl for this type")
851859
.note("only trait implementations may be annotated with `const`")
852860
.emit();
853861
}

src/librustc_ast_passes/feature_gate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
338338
}
339339
}
340340

341-
ast::ItemKind::Impl { polarity, defaultness, .. } => {
342-
if polarity == ast::ImplPolarity::Negative {
341+
ast::ItemKind::Impl { polarity, defaultness, ref of_trait, .. } => {
342+
if let ast::ImplPolarity::Negative(span) = polarity {
343343
gate_feature_post!(
344344
&self,
345345
optin_builtin_traits,
346-
i.span,
346+
span.to(of_trait.as_ref().map(|t| t.path.span).unwrap_or(span)),
347347
"negative trait bounds are not yet fully implemented; \
348-
use marker types for now"
348+
use marker types for now"
349349
);
350350
}
351351

src/librustc_ast_pretty/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ impl<'a> State<'a> {
11751175
self.s.space();
11761176
}
11771177

1178-
if polarity == ast::ImplPolarity::Negative {
1178+
if let ast::ImplPolarity::Negative(_) = polarity {
11791179
self.s.word("!");
11801180
}
11811181

src/librustc_hir/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ impl<'a> State<'a> {
652652
self.word_nbsp("const");
653653
}
654654

655-
if let hir::ImplPolarity::Negative = polarity {
655+
if let hir::ImplPolarity::Negative(_) = polarity {
656656
self.s.word("!");
657657
}
658658

src/librustc_parse/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl<'a> Parser<'a> {
414414
// Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
415415
let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
416416
self.bump(); // `!`
417-
ast::ImplPolarity::Negative
417+
ast::ImplPolarity::Negative(self.prev_token.span)
418418
} else {
419419
ast::ImplPolarity::Positive
420420
};

src/librustc_save_analysis/sig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl Sig for ast::Item {
519519
text.push(' ');
520520

521521
let trait_sig = if let Some(ref t) = *of_trait {
522-
if polarity == ast::ImplPolarity::Negative {
522+
if let ast::ImplPolarity::Negative(_) = polarity {
523523
text.push('!');
524524
}
525525
let trait_sig = t.path.make(offset + text.len(), id, scx)?;

src/librustc_typeck/coherence/unsafety.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ impl UnsafetyChecker<'tcx> {
6969
.emit();
7070
}
7171

72-
(_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => {
72+
(_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
7373
// Reported in AST validation
7474
self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
7575
}
76-
(_, _, Unsafety::Normal, hir::ImplPolarity::Negative)
76+
(_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
7777
| (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
7878
| (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
7979
| (Unsafety::Normal, None, Unsafety::Normal, _) => {

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
15481548
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
15491549
let item = tcx.hir().expect_item(hir_id);
15501550
match &item.kind {
1551-
hir::ItemKind::Impl { polarity: hir::ImplPolarity::Negative, .. } => {
1551+
hir::ItemKind::Impl { polarity: hir::ImplPolarity::Negative(_), .. } => {
15521552
if is_rustc_reservation {
15531553
tcx.sess.span_err(item.span, "reservation impls can't be negative");
15541554
}

src/test/ui/coherence/coherence-negative-impls-safe.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0198]: negative impls cannot be unsafe
2-
--> $DIR/coherence-negative-impls-safe.rs:7:1
2+
--> $DIR/coherence-negative-impls-safe.rs:7:13
33
|
44
LL | unsafe impl !Send for TestType {}
5-
| ------^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ------ ^^^^^
66
| |
77
| unsafe because of this
88

src/test/ui/error-codes/E0197.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0197]: inherent impls cannot be unsafe
22
--> $DIR/E0197.rs:3:1
33
|
44
LL | unsafe impl Foo { }
5-
| ------^^^^^^^^^^^^^
5+
| ^^^^^^ ^^^ inherent impl for this type
66
| |
77
| unsafe because of this
88

src/test/ui/error-codes/E0198.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0198]: negative impls cannot be unsafe
2-
--> $DIR/E0198.rs:5:1
2+
--> $DIR/E0198.rs:5:13
33
|
44
LL | unsafe impl !Send for Foo { }
5-
| ------^^^^^^^^^^^^^^^^^^^^^^^
5+
| ------ ^^^^^
66
| |
77
| unsafe because of this
88

src/test/ui/feature-gates/feature-gate-optin-builtin-traits.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | auto trait AutoDummyTrait {}
88
= help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable
99

1010
error[E0658]: negative trait bounds are not yet fully implemented; use marker types for now
11-
--> $DIR/feature-gate-optin-builtin-traits.rs:9:1
11+
--> $DIR/feature-gate-optin-builtin-traits.rs:9:6
1212
|
1313
LL | impl !AutoDummyTrait for DummyStruct {}
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^
1515
|
1616
= note: see issue #13231 <https://github.com/rust-lang/rust/issues/13231> for more information
1717
= help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable

src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: inherent impls cannot be `const`
2-
--> $DIR/inherent-impl.rs:9:1
2+
--> $DIR/inherent-impl.rs:9:6
33
|
44
LL | impl const S {}
5-
| ^^^^^-----^^^^^
5+
| ^^^^^ ^ inherent impl for this type
66
| |
77
| `const` because of this
88
|
99
= note: only trait implementations may be annotated with `const`
1010

1111
error: inherent impls cannot be `const`
12-
--> $DIR/inherent-impl.rs:12:1
12+
--> $DIR/inherent-impl.rs:12:6
1313
|
1414
LL | impl const T {}
15-
| ^^^^^-----^^^^^
15+
| ^^^^^ ^ inherent impl for this type
1616
| |
1717
| `const` because of this
1818
|

src/test/ui/specialization/defaultimpl/validation.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: inherent impls cannot be `default`
22
--> $DIR/validation.rs:7:1
33
|
44
LL | default impl S {}
5-
| -------^^^^^^^
5+
| ^^^^^^^ ^ inherent impl for this type
66
| |
77
| `default` because of this
88
|

src/test/ui/syntax-trait-polarity-feature-gate.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: negative trait bounds are not yet fully implemented; use marker types for now
2-
--> $DIR/syntax-trait-polarity-feature-gate.rs:7:1
2+
--> $DIR/syntax-trait-polarity-feature-gate.rs:7:6
33
|
44
LL | impl !Send for TestType {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^
66
|
77
= note: see issue #13231 <https://github.com/rust-lang/rust/issues/13231> for more information
88
= help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable

src/test/ui/syntax-trait-polarity.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
error: inherent impls cannot be negative
2-
--> $DIR/syntax-trait-polarity.rs:7:1
2+
--> $DIR/syntax-trait-polarity.rs:7:6
33
|
44
LL | impl !TestType {}
5-
| ^^^^^^^^^^^^^^^^^
5+
| ^
66

77
error[E0198]: negative impls cannot be unsafe
8-
--> $DIR/syntax-trait-polarity.rs:12:1
8+
--> $DIR/syntax-trait-polarity.rs:12:13
99
|
1010
LL | unsafe impl !Send for TestType {}
11-
| ------^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ------ ^^^^^
1212
| |
1313
| unsafe because of this
1414

1515
error: inherent impls cannot be negative
16-
--> $DIR/syntax-trait-polarity.rs:19:1
16+
--> $DIR/syntax-trait-polarity.rs:19:9
1717
|
1818
LL | impl<T> !TestType2<T> {}
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^
19+
| ^
2020

2121
error[E0198]: negative impls cannot be unsafe
22-
--> $DIR/syntax-trait-polarity.rs:22:1
22+
--> $DIR/syntax-trait-polarity.rs:22:16
2323
|
2424
LL | unsafe impl<T> !Send for TestType2<T> {}
25-
| ------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
| ------ ^^^^^
2626
| |
2727
| unsafe because of this
2828

src/test/ui/traits/trait-safety-inherent-impl.stderr

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
error[E0197]: inherent impls cannot be unsafe
22
--> $DIR/trait-safety-inherent-impl.rs:5:1
33
|
4-
LL | unsafe impl SomeStruct {
5-
| ^-----
6-
| |
7-
| _unsafe because of this
4+
LL | unsafe impl SomeStruct {
5+
| ^^^^^^ ^^^^^^^^^^ inherent impl for this type
86
| |
9-
LL | | fn foo(self) { }
10-
LL | | }
11-
| |_^
7+
| unsafe because of this
128

139
error: aborting due to previous error
1410

0 commit comments

Comments
 (0)