Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c9158db

Browse files
authoredOct 5, 2021
Rollup merge of #89494 - FabianWolff:issue-84075, r=davidtwco
Deny `where` clauses on `auto` traits Fixes #84075.
2 parents 90e96f9 + e34fd54 commit c9158db

12 files changed

+135
-47
lines changed
 

‎compiler/rustc_ast_passes/src/ast_validation.rs

+36-13
Original file line numberDiff line numberDiff line change
@@ -683,31 +683,53 @@ impl<'a> AstValidator<'a> {
683683
}
684684
}
685685

686+
fn emit_e0568(&self, span: Span, ident_span: Span) {
687+
struct_span_err!(
688+
self.session,
689+
span,
690+
E0568,
691+
"auto traits cannot have super traits or lifetime bounds"
692+
)
693+
.span_label(ident_span, "auto trait cannot have super traits or lifetime bounds")
694+
.span_suggestion(
695+
span,
696+
"remove the super traits or lifetime bounds",
697+
String::new(),
698+
Applicability::MachineApplicable,
699+
)
700+
.emit();
701+
}
702+
686703
fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
687-
if let [first @ last] | [first, .., last] = &bounds[..] {
688-
let span = first.span().to(last.span());
689-
struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits")
690-
.span_label(ident_span, "auto trait cannot have super traits")
691-
.span_suggestion(
692-
span,
693-
"remove the super traits",
694-
String::new(),
695-
Applicability::MachineApplicable,
696-
)
697-
.emit();
704+
if let [.., last] = &bounds[..] {
705+
let span = ident_span.shrink_to_hi().to(last.span());
706+
self.emit_e0568(span, ident_span);
707+
}
708+
}
709+
710+
fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
711+
if !where_clause.predicates.is_empty() {
712+
self.emit_e0568(where_clause.span, ident_span);
698713
}
699714
}
700715

701716
fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
702717
if !trait_items.is_empty() {
703718
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
719+
let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
704720
struct_span_err!(
705721
self.session,
706722
spans,
707723
E0380,
708-
"auto traits cannot have methods or associated items"
724+
"auto traits cannot have associated items"
725+
)
726+
.span_suggestion(
727+
total_span,
728+
"remove these associated items",
729+
String::new(),
730+
Applicability::MachineApplicable,
709731
)
710-
.span_label(ident_span, "auto trait cannot have items")
732+
.span_label(ident_span, "auto trait cannot have associated items")
711733
.emit();
712734
}
713735
}
@@ -1184,6 +1206,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11841206
// Auto traits cannot have generics, super traits nor contain items.
11851207
self.deny_generic_params(generics, item.ident.span);
11861208
self.deny_super_traits(bounds, item.ident.span);
1209+
self.deny_where_clause(&generics.where_clause, item.ident.span);
11871210
self.deny_items(trait_items, item.ident.span);
11881211
}
11891212
self.no_questions_in_bounds(bounds, "supertraits", true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(auto_traits)]
2+
3+
// run-rustfix
4+
5+
auto trait Generic {}
6+
//~^ auto traits cannot have generic parameters [E0567]
7+
auto trait Bound {}
8+
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
9+
auto trait LifetimeBound {}
10+
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
11+
auto trait MyTrait { }
12+
//~^ auto traits cannot have associated items [E0380]
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#![feature(auto_traits)]
22

3+
// run-rustfix
4+
35
auto trait Generic<T> {}
46
//~^ auto traits cannot have generic parameters [E0567]
57
auto trait Bound : Copy {}
6-
//~^ auto traits cannot have super traits [E0568]
8+
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
9+
auto trait LifetimeBound : 'static {}
10+
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
711
auto trait MyTrait { fn foo() {} }
8-
//~^ auto traits cannot have methods or associated items [E0380]
12+
//~^ auto traits cannot have associated items [E0380]
913
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
error[E0567]: auto traits cannot have generic parameters
2-
--> $DIR/auto-trait-validation.rs:3:19
2+
--> $DIR/auto-trait-validation.rs:5:19
33
|
44
LL | auto trait Generic<T> {}
55
| -------^^^ help: remove the parameters
66
| |
77
| auto trait cannot have generic parameters
88

9-
error[E0568]: auto traits cannot have super traits
10-
--> $DIR/auto-trait-validation.rs:5:20
9+
error[E0568]: auto traits cannot have super traits or lifetime bounds
10+
--> $DIR/auto-trait-validation.rs:7:17
1111
|
1212
LL | auto trait Bound : Copy {}
13-
| ----- ^^^^ help: remove the super traits
13+
| -----^^^^^^^ help: remove the super traits or lifetime bounds
1414
| |
15-
| auto trait cannot have super traits
15+
| auto trait cannot have super traits or lifetime bounds
1616

17-
error[E0380]: auto traits cannot have methods or associated items
18-
--> $DIR/auto-trait-validation.rs:7:25
17+
error[E0568]: auto traits cannot have super traits or lifetime bounds
18+
--> $DIR/auto-trait-validation.rs:9:25
1919
|
20-
LL | auto trait MyTrait { fn foo() {} }
21-
| ------- ^^^
20+
LL | auto trait LifetimeBound : 'static {}
21+
| -------------^^^^^^^^^^ help: remove the super traits or lifetime bounds
2222
| |
23-
| auto trait cannot have items
23+
| auto trait cannot have super traits or lifetime bounds
24+
25+
error[E0380]: auto traits cannot have associated items
26+
--> $DIR/auto-trait-validation.rs:11:25
27+
|
28+
LL | auto trait MyTrait { fn foo() {} }
29+
| ------- ---^^^-----
30+
| | |
31+
| | help: remove these associated items
32+
| auto trait cannot have associated items
2433

25-
error: aborting due to 3 previous errors
34+
error: aborting due to 4 previous errors
2635

2736
Some errors have detailed explanations: E0380, E0567, E0568.
2837
For more information about an error, try `rustc --explain E0380`.

‎src/test/ui/auto-traits/issue-23080-2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0380]: auto traits cannot have methods or associated items
1+
error[E0380]: auto traits cannot have associated items
22
--> $DIR/issue-23080-2.rs:5:10
33
|
44
LL | unsafe auto trait Trait {
5-
| ----- auto trait cannot have items
5+
| ----- auto trait cannot have associated items
66
LL | type Output;
7-
| ^^^^^^
7+
| -----^^^^^^- help: remove these associated items
88

99
error: aborting due to previous error
1010

‎src/test/ui/auto-traits/issue-23080.stderr

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
error[E0380]: auto traits cannot have methods or associated items
1+
error[E0380]: auto traits cannot have associated items
22
--> $DIR/issue-23080.rs:5:8
33
|
4-
LL | unsafe auto trait Trait {
5-
| ----- auto trait cannot have items
6-
LL | fn method(&self) {
7-
| ^^^^^^
4+
LL | unsafe auto trait Trait {
5+
| ----- auto trait cannot have associated items
6+
LL | fn method(&self) {
7+
| _____- ^^^^^^
8+
LL | | println!("Hello");
9+
LL | | }
10+
| |_____- help: remove these associated items
811

912
error: aborting due to previous error
1013

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for issue #84075.
2+
3+
#![feature(auto_traits)]
4+
5+
auto trait Magic where Self: Copy {} //~ ERROR E0568
6+
impl<T: Magic> Magic for T {}
7+
8+
fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
9+
10+
#[derive(Debug)]
11+
struct NoClone;
12+
13+
fn main() {
14+
let (a, b) = copy(NoClone);
15+
println!("{:?} {:?}", a, b);
16+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0568]: auto traits cannot have super traits or lifetime bounds
2+
--> $DIR/issue-84075.rs:5:18
3+
|
4+
LL | auto trait Magic where Self: Copy {}
5+
| ----- ^^^^^^^^^^^^^^^^ help: remove the super traits or lifetime bounds
6+
| |
7+
| auto trait cannot have super traits or lifetime bounds
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0568`.

‎src/test/ui/auto-traits/typeck-auto-trait-no-supertraits-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(negative_impls)]
33

44
auto trait Magic : Sized where Option<Self> : Magic {} //~ ERROR E0568
5+
//~^ ERROR E0568
56
impl<T:Magic> Magic for T {}
67

78
fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:20
1+
error[E0568]: auto traits cannot have super traits or lifetime bounds
2+
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:17
33
|
44
LL | auto trait Magic : Sized where Option<Self> : Magic {}
5-
| ----- ^^^^^ help: remove the super traits
5+
| -----^^^^^^^^ help: remove the super traits or lifetime bounds
66
| |
7-
| auto trait cannot have super traits
7+
| auto trait cannot have super traits or lifetime bounds
88

9-
error: aborting due to previous error
9+
error[E0568]: auto traits cannot have super traits or lifetime bounds
10+
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:26
11+
|
12+
LL | auto trait Magic : Sized where Option<Self> : Magic {}
13+
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the super traits or lifetime bounds
14+
| |
15+
| auto trait cannot have super traits or lifetime bounds
16+
17+
error: aborting due to 2 previous errors
1018

1119
For more information about this error, try `rustc --explain E0568`.

‎src/test/ui/auto-traits/typeck-auto-trait-no-supertraits.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/typeck-auto-trait-no-supertraits.rs:28:19
1+
error[E0568]: auto traits cannot have super traits or lifetime bounds
2+
--> $DIR/typeck-auto-trait-no-supertraits.rs:28:17
33
|
44
LL | auto trait Magic: Copy {}
5-
| ----- ^^^^ help: remove the super traits
5+
| -----^^^^^^ help: remove the super traits or lifetime bounds
66
| |
7-
| auto trait cannot have super traits
7+
| auto trait cannot have super traits or lifetime bounds
88

99
error: aborting due to previous error
1010

‎src/test/ui/traits/inductive-overflow/supertrait-auto-trait.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0568]: auto traits cannot have super traits
2-
--> $DIR/supertrait-auto-trait.rs:8:19
1+
error[E0568]: auto traits cannot have super traits or lifetime bounds
2+
--> $DIR/supertrait-auto-trait.rs:8:17
33
|
44
LL | auto trait Magic: Copy {}
5-
| ----- ^^^^ help: remove the super traits
5+
| -----^^^^^^ help: remove the super traits or lifetime bounds
66
| |
7-
| auto trait cannot have super traits
7+
| auto trait cannot have super traits or lifetime bounds
88

99
error[E0277]: the trait bound `NoClone: Copy` is not satisfied
1010
--> $DIR/supertrait-auto-trait.rs:16:23

0 commit comments

Comments
 (0)
Failed to load comments.