From 91525fd078bb6f3ec833aa42141ea027b37b26a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 4 Mar 2020 16:15:23 -0800 Subject: [PATCH] Tweak output for invalid negative impl AST errors --- src/librustc_ast/ast.rs | 4 +-- src/librustc_ast_passes/ast_validation.rs | 28 ++++++++++++------- src/librustc_ast_passes/feature_gate.rs | 8 +++--- src/librustc_ast_pretty/pprust.rs | 2 +- src/librustc_hir/print.rs | 2 +- src/librustc_parse/parser/item.rs | 2 +- src/librustc_save_analysis/sig.rs | 2 +- src/librustc_typeck/coherence/unsafety.rs | 4 +-- src/librustc_typeck/collect.rs | 2 +- .../coherence-negative-impls-safe.stderr | 4 +-- src/test/ui/error-codes/E0197.stderr | 2 +- src/test/ui/error-codes/E0198.stderr | 4 +-- .../feature-gate-optin-builtin-traits.stderr | 4 +-- .../inherent-impl.stderr | 8 +++--- .../defaultimpl/validation.stderr | 2 +- .../syntax-trait-polarity-feature-gate.stderr | 4 +-- src/test/ui/syntax-trait-polarity.stderr | 16 +++++------ .../traits/trait-safety-inherent-impl.stderr | 10 ++----- 18 files changed, 56 insertions(+), 52 deletions(-) diff --git a/src/librustc_ast/ast.rs b/src/librustc_ast/ast.rs index 88564647d61..e86ec2ebfa4 100644 --- a/src/librustc_ast/ast.rs +++ b/src/librustc_ast/ast.rs @@ -2117,14 +2117,14 @@ pub enum ImplPolarity { /// `impl Trait for Type` Positive, /// `impl !Trait for Type` - Negative, + Negative(Span), } impl fmt::Debug for ImplPolarity { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { ImplPolarity::Positive => "positive".fmt(f), - ImplPolarity::Negative => "negative".fmt(f), + ImplPolarity::Negative(_) => "negative".fmt(f), } } } diff --git a/src/librustc_ast_passes/ast_validation.rs b/src/librustc_ast_passes/ast_validation.rs index 9f04c01bfa8..1070043458a 100644 --- a/src/librustc_ast_passes/ast_validation.rs +++ b/src/librustc_ast_passes/ast_validation.rs @@ -779,7 +779,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { defaultness: _, constness: _, generics: _, - of_trait: Some(_), + of_trait: Some(ref t), ref self_ty, items: _, } => { @@ -794,10 +794,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .help("use `auto trait Trait {}` instead") .emit(); } - if let (Unsafe::Yes(span), ImplPolarity::Negative) = (unsafety, polarity) { + if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) { struct_span_err!( this.session, - item.span, + sp.to(t.path.span), E0198, "negative impls cannot be unsafe" ) @@ -816,7 +816,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { constness, generics: _, of_trait: None, - self_ty: _, + ref self_ty, items: _, } => { self.invalid_visibility( @@ -826,28 +826,36 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if let Unsafe::Yes(span) = unsafety { struct_span_err!( self.session, - item.span, + vec![span, self_ty.span], E0197, "inherent impls cannot be unsafe" ) .span_label(span, "unsafe because of this") + .span_label(self_ty.span, "inherent impl for this type") .emit(); } - if polarity == ImplPolarity::Negative { - self.err_handler().span_err(item.span, "inherent impls cannot be negative"); + if let ImplPolarity::Negative(span) = polarity { + self.err_handler().span_err(span, "inherent impls cannot be negative"); } if let Defaultness::Default(def_span) = defaultness { - let span = self.session.source_map().def_span(item.span); self.err_handler() - .struct_span_err(span, "inherent impls cannot be `default`") + .struct_span_err( + vec![def_span, self_ty.span], + "inherent impls cannot be `default`", + ) .span_label(def_span, "`default` because of this") + .span_label(self_ty.span, "inherent impl for this type") .note("only trait implementations may be annotated with `default`") .emit(); } if let Const::Yes(span) = constness { self.err_handler() - .struct_span_err(item.span, "inherent impls cannot be `const`") + .struct_span_err( + vec![span, self_ty.span], + "inherent impls cannot be `const`", + ) .span_label(span, "`const` because of this") + .span_label(self_ty.span, "inherent impl for this type") .note("only trait implementations may be annotated with `const`") .emit(); } diff --git a/src/librustc_ast_passes/feature_gate.rs b/src/librustc_ast_passes/feature_gate.rs index 05e69d0cfd7..3d9001d5d58 100644 --- a/src/librustc_ast_passes/feature_gate.rs +++ b/src/librustc_ast_passes/feature_gate.rs @@ -338,14 +338,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - ast::ItemKind::Impl { polarity, defaultness, .. } => { - if polarity == ast::ImplPolarity::Negative { + ast::ItemKind::Impl { polarity, defaultness, ref of_trait, .. } => { + if let ast::ImplPolarity::Negative(span) = polarity { gate_feature_post!( &self, optin_builtin_traits, - i.span, + span.to(of_trait.as_ref().map(|t| t.path.span).unwrap_or(span)), "negative trait bounds are not yet fully implemented; \ - use marker types for now" + use marker types for now" ); } diff --git a/src/librustc_ast_pretty/pprust.rs b/src/librustc_ast_pretty/pprust.rs index f95c154bb3b..dbfe5d34bc0 100644 --- a/src/librustc_ast_pretty/pprust.rs +++ b/src/librustc_ast_pretty/pprust.rs @@ -1175,7 +1175,7 @@ impl<'a> State<'a> { self.s.space(); } - if polarity == ast::ImplPolarity::Negative { + if let ast::ImplPolarity::Negative(_) = polarity { self.s.word("!"); } diff --git a/src/librustc_hir/print.rs b/src/librustc_hir/print.rs index 8cbbef959ce..f03ab41f12d 100644 --- a/src/librustc_hir/print.rs +++ b/src/librustc_hir/print.rs @@ -652,7 +652,7 @@ impl<'a> State<'a> { self.word_nbsp("const"); } - if let hir::ImplPolarity::Negative = polarity { + if let hir::ImplPolarity::Negative(_) = polarity { self.s.word("!"); } diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 9bca1d09901..09c512f3254 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -414,7 +414,7 @@ impl<'a> Parser<'a> { // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type. let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) { self.bump(); // `!` - ast::ImplPolarity::Negative + ast::ImplPolarity::Negative(self.prev_token.span) } else { ast::ImplPolarity::Positive }; diff --git a/src/librustc_save_analysis/sig.rs b/src/librustc_save_analysis/sig.rs index 32da62adc3c..4fa226712b7 100644 --- a/src/librustc_save_analysis/sig.rs +++ b/src/librustc_save_analysis/sig.rs @@ -519,7 +519,7 @@ impl Sig for ast::Item { text.push(' '); let trait_sig = if let Some(ref t) = *of_trait { - if polarity == ast::ImplPolarity::Negative { + if let ast::ImplPolarity::Negative(_) = polarity { text.push('!'); } let trait_sig = t.path.make(offset + text.len(), id, scx)?; diff --git a/src/librustc_typeck/coherence/unsafety.rs b/src/librustc_typeck/coherence/unsafety.rs index a6044217403..3b25f67aacc 100644 --- a/src/librustc_typeck/coherence/unsafety.rs +++ b/src/librustc_typeck/coherence/unsafety.rs @@ -69,11 +69,11 @@ impl UnsafetyChecker<'tcx> { .emit(); } - (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative) => { + (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { // Reported in AST validation self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl"); } - (_, _, Unsafety::Normal, hir::ImplPolarity::Negative) + (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) | (Unsafety::Normal, None, Unsafety::Normal, _) => { diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 2dad3d1d6d7..e6c3d5b8b9e 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1548,7 +1548,7 @@ fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl); let item = tcx.hir().expect_item(hir_id); match &item.kind { - hir::ItemKind::Impl { polarity: hir::ImplPolarity::Negative, .. } => { + hir::ItemKind::Impl { polarity: hir::ImplPolarity::Negative(_), .. } => { if is_rustc_reservation { tcx.sess.span_err(item.span, "reservation impls can't be negative"); } diff --git a/src/test/ui/coherence/coherence-negative-impls-safe.stderr b/src/test/ui/coherence/coherence-negative-impls-safe.stderr index 4db66af6783..eebd1de277e 100644 --- a/src/test/ui/coherence/coherence-negative-impls-safe.stderr +++ b/src/test/ui/coherence/coherence-negative-impls-safe.stderr @@ -1,8 +1,8 @@ error[E0198]: negative impls cannot be unsafe - --> $DIR/coherence-negative-impls-safe.rs:7:1 + --> $DIR/coherence-negative-impls-safe.rs:7:13 | LL | unsafe impl !Send for TestType {} - | ------^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ------ ^^^^^ | | | unsafe because of this diff --git a/src/test/ui/error-codes/E0197.stderr b/src/test/ui/error-codes/E0197.stderr index 51ed9c83bc9..bc3c2857958 100644 --- a/src/test/ui/error-codes/E0197.stderr +++ b/src/test/ui/error-codes/E0197.stderr @@ -2,7 +2,7 @@ error[E0197]: inherent impls cannot be unsafe --> $DIR/E0197.rs:3:1 | LL | unsafe impl Foo { } - | ------^^^^^^^^^^^^^ + | ^^^^^^ ^^^ inherent impl for this type | | | unsafe because of this diff --git a/src/test/ui/error-codes/E0198.stderr b/src/test/ui/error-codes/E0198.stderr index 90e8b4abd12..43304768700 100644 --- a/src/test/ui/error-codes/E0198.stderr +++ b/src/test/ui/error-codes/E0198.stderr @@ -1,8 +1,8 @@ error[E0198]: negative impls cannot be unsafe - --> $DIR/E0198.rs:5:1 + --> $DIR/E0198.rs:5:13 | LL | unsafe impl !Send for Foo { } - | ------^^^^^^^^^^^^^^^^^^^^^^^ + | ------ ^^^^^ | | | unsafe because of this diff --git a/src/test/ui/feature-gates/feature-gate-optin-builtin-traits.stderr b/src/test/ui/feature-gates/feature-gate-optin-builtin-traits.stderr index d29c373a33c..490d29ad8a3 100644 --- a/src/test/ui/feature-gates/feature-gate-optin-builtin-traits.stderr +++ b/src/test/ui/feature-gates/feature-gate-optin-builtin-traits.stderr @@ -8,10 +8,10 @@ LL | auto trait AutoDummyTrait {} = help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable error[E0658]: negative trait bounds are not yet fully implemented; use marker types for now - --> $DIR/feature-gate-optin-builtin-traits.rs:9:1 + --> $DIR/feature-gate-optin-builtin-traits.rs:9:6 | LL | impl !AutoDummyTrait for DummyStruct {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ | = note: see issue #13231 for more information = help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable diff --git a/src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr b/src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr index 3ea58a3728a..0a76c70b97d 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr @@ -1,18 +1,18 @@ error: inherent impls cannot be `const` - --> $DIR/inherent-impl.rs:9:1 + --> $DIR/inherent-impl.rs:9:6 | LL | impl const S {} - | ^^^^^-----^^^^^ + | ^^^^^ ^ inherent impl for this type | | | `const` because of this | = note: only trait implementations may be annotated with `const` error: inherent impls cannot be `const` - --> $DIR/inherent-impl.rs:12:1 + --> $DIR/inherent-impl.rs:12:6 | LL | impl const T {} - | ^^^^^-----^^^^^ + | ^^^^^ ^ inherent impl for this type | | | `const` because of this | diff --git a/src/test/ui/specialization/defaultimpl/validation.stderr b/src/test/ui/specialization/defaultimpl/validation.stderr index 03b1ef69ca0..2a96f41a249 100644 --- a/src/test/ui/specialization/defaultimpl/validation.stderr +++ b/src/test/ui/specialization/defaultimpl/validation.stderr @@ -2,7 +2,7 @@ error: inherent impls cannot be `default` --> $DIR/validation.rs:7:1 | LL | default impl S {} - | -------^^^^^^^ + | ^^^^^^^ ^ inherent impl for this type | | | `default` because of this | diff --git a/src/test/ui/syntax-trait-polarity-feature-gate.stderr b/src/test/ui/syntax-trait-polarity-feature-gate.stderr index ed76377278b..5d4c1b354f7 100644 --- a/src/test/ui/syntax-trait-polarity-feature-gate.stderr +++ b/src/test/ui/syntax-trait-polarity-feature-gate.stderr @@ -1,8 +1,8 @@ error[E0658]: negative trait bounds are not yet fully implemented; use marker types for now - --> $DIR/syntax-trait-polarity-feature-gate.rs:7:1 + --> $DIR/syntax-trait-polarity-feature-gate.rs:7:6 | LL | impl !Send for TestType {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ | = note: see issue #13231 for more information = help: add `#![feature(optin_builtin_traits)]` to the crate attributes to enable diff --git a/src/test/ui/syntax-trait-polarity.stderr b/src/test/ui/syntax-trait-polarity.stderr index fef3a650888..b7d5b4570aa 100644 --- a/src/test/ui/syntax-trait-polarity.stderr +++ b/src/test/ui/syntax-trait-polarity.stderr @@ -1,28 +1,28 @@ error: inherent impls cannot be negative - --> $DIR/syntax-trait-polarity.rs:7:1 + --> $DIR/syntax-trait-polarity.rs:7:6 | LL | impl !TestType {} - | ^^^^^^^^^^^^^^^^^ + | ^ error[E0198]: negative impls cannot be unsafe - --> $DIR/syntax-trait-polarity.rs:12:1 + --> $DIR/syntax-trait-polarity.rs:12:13 | LL | unsafe impl !Send for TestType {} - | ------^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ------ ^^^^^ | | | unsafe because of this error: inherent impls cannot be negative - --> $DIR/syntax-trait-polarity.rs:19:1 + --> $DIR/syntax-trait-polarity.rs:19:9 | LL | impl !TestType2 {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^ error[E0198]: negative impls cannot be unsafe - --> $DIR/syntax-trait-polarity.rs:22:1 + --> $DIR/syntax-trait-polarity.rs:22:16 | LL | unsafe impl !Send for TestType2 {} - | ------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ------ ^^^^^ | | | unsafe because of this diff --git a/src/test/ui/traits/trait-safety-inherent-impl.stderr b/src/test/ui/traits/trait-safety-inherent-impl.stderr index c398785d394..09ad4585ab1 100644 --- a/src/test/ui/traits/trait-safety-inherent-impl.stderr +++ b/src/test/ui/traits/trait-safety-inherent-impl.stderr @@ -1,14 +1,10 @@ error[E0197]: inherent impls cannot be unsafe --> $DIR/trait-safety-inherent-impl.rs:5:1 | -LL | unsafe impl SomeStruct { - | ^----- - | | - | _unsafe because of this +LL | unsafe impl SomeStruct { + | ^^^^^^ ^^^^^^^^^^ inherent impl for this type | | -LL | | fn foo(self) { } -LL | | } - | |_^ + | unsafe because of this error: aborting due to previous error