Skip to content

Commit 713a291

Browse files
committed
review comments
1 parent 91525fd commit 713a291

File tree

9 files changed

+65
-57
lines changed

9 files changed

+65
-57
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
801801
E0198,
802802
"negative impls cannot be unsafe"
803803
)
804+
.span_label(sp, "negative because of this")
804805
.span_label(span, "unsafe because of this")
805806
.emit();
806807
}
@@ -819,45 +820,40 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
819820
ref self_ty,
820821
items: _,
821822
} => {
823+
let error = |annotation_span, annotation, note, code| {
824+
let mut err = self.err_handler().struct_span_err(
825+
self_ty.span,
826+
&format!("inherent impls cannot be {}", annotation),
827+
);
828+
err.span_label(annotation_span, &format!("{} because of this", annotation));
829+
err.span_label(self_ty.span, "inherent impl for this type");
830+
if note {
831+
err.note(&format!(
832+
"only trait implementations may be annotated with {}",
833+
annotation
834+
));
835+
}
836+
if code {
837+
err.code(error_code!(E0197));
838+
}
839+
err.emit();
840+
};
841+
822842
self.invalid_visibility(
823843
&item.vis,
824844
Some("place qualifiers on individual impl items instead"),
825845
);
826846
if let Unsafe::Yes(span) = unsafety {
827-
struct_span_err!(
828-
self.session,
829-
vec![span, self_ty.span],
830-
E0197,
831-
"inherent impls cannot be unsafe"
832-
)
833-
.span_label(span, "unsafe because of this")
834-
.span_label(self_ty.span, "inherent impl for this type")
835-
.emit();
847+
error(span, "unsafe", false, true)
836848
}
837849
if let ImplPolarity::Negative(span) = polarity {
838-
self.err_handler().span_err(span, "inherent impls cannot be negative");
850+
error(span, "negative", false, false);
839851
}
840852
if let Defaultness::Default(def_span) = defaultness {
841-
self.err_handler()
842-
.struct_span_err(
843-
vec![def_span, self_ty.span],
844-
"inherent impls cannot be `default`",
845-
)
846-
.span_label(def_span, "`default` because of this")
847-
.span_label(self_ty.span, "inherent impl for this type")
848-
.note("only trait implementations may be annotated with `default`")
849-
.emit();
853+
error(def_span, "`default`", true, false);
850854
}
851855
if let Const::Yes(span) = constness {
852-
self.err_handler()
853-
.struct_span_err(
854-
vec![span, self_ty.span],
855-
"inherent impls cannot be `const`",
856-
)
857-
.span_label(span, "`const` because of this")
858-
.span_label(self_ty.span, "inherent impl for this type")
859-
.note("only trait implementations may be annotated with `const`")
860-
.emit();
856+
error(span, "`const`", true, false);
861857
}
862858
}
863859
ItemKind::Fn(def, ref sig, ref generics, ref body) => {

src/librustc_parse/parser/item.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ impl<'a> Parser<'a> {
373373
self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
374374
}
375375

376+
fn parse_polarity(&mut self) -> ast::ImplPolarity {
377+
// Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
378+
if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
379+
self.bump(); // `!`
380+
ast::ImplPolarity::Negative(self.prev_token.span)
381+
} else {
382+
ast::ImplPolarity::Positive
383+
}
384+
}
385+
376386
/// Parses an implementation item.
377387
///
378388
/// ```
@@ -411,13 +421,7 @@ impl<'a> Parser<'a> {
411421
self.sess.gated_spans.gate(sym::const_trait_impl, span);
412422
}
413423

414-
// Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
415-
let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
416-
self.bump(); // `!`
417-
ast::ImplPolarity::Negative(self.prev_token.span)
418-
} else {
419-
ast::ImplPolarity::Positive
420-
};
424+
let polarity = self.parse_polarity();
421425

422426
// Parse both types and traits as a type, then reinterpret if necessary.
423427
let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));

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

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

910
error: aborting due to previous error

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

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

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

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

910
error: aborting due to previous error

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:6
2+
--> $DIR/inherent-impl.rs:9:12
33
|
44
LL | impl const S {}
5-
| ^^^^^ ^ inherent impl for this type
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:6
12+
--> $DIR/inherent-impl.rs:12:12
1313
|
1414
LL | impl const T {}
15-
| ^^^^^ ^ inherent impl for this type
15+
| ----- ^ inherent impl for this type
1616
| |
1717
| `const` because of this
1818
|

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

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

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

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

79
error[E0198]: negative impls cannot be unsafe
810
--> $DIR/syntax-trait-polarity.rs:12:13
911
|
1012
LL | unsafe impl !Send for TestType {}
11-
| ------ ^^^^^
12-
| |
13+
| ------ -^^^^
14+
| | |
15+
| | negative because of this
1316
| unsafe because of this
1417

1518
error: inherent impls cannot be negative
16-
--> $DIR/syntax-trait-polarity.rs:19:9
19+
--> $DIR/syntax-trait-polarity.rs:19:10
1720
|
1821
LL | impl<T> !TestType2<T> {}
19-
| ^
22+
| -^^^^^^^^^^^^ inherent impl for this type
23+
| |
24+
| negative because of this
2025

2126
error[E0198]: negative impls cannot be unsafe
2227
--> $DIR/syntax-trait-polarity.rs:22:16
2328
|
2429
LL | unsafe impl<T> !Send for TestType2<T> {}
25-
| ------ ^^^^^
26-
| |
30+
| ------ -^^^^
31+
| | |
32+
| | negative because of this
2733
| unsafe because of this
2834

2935
error[E0192]: negative impls are only allowed for auto traits (e.g., `Send` and `Sync`)

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

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

0 commit comments

Comments
 (0)