Skip to content

Commit

Permalink
Deny where clauses on auto traits
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianWolff committed Oct 3, 2021
1 parent edebf77 commit e34fd54
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 47 deletions.
49 changes: 36 additions & 13 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Expand Up @@ -683,31 +683,53 @@ impl<'a> AstValidator<'a> {
}
}

fn emit_e0568(&self, span: Span, ident_span: Span) {
struct_span_err!(
self.session,
span,
E0568,
"auto traits cannot have super traits or lifetime bounds"
)
.span_label(ident_span, "auto trait cannot have super traits or lifetime bounds")
.span_suggestion(
span,
"remove the super traits or lifetime bounds",
String::new(),
Applicability::MachineApplicable,
)
.emit();
}

fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
if let [first @ last] | [first, .., last] = &bounds[..] {
let span = first.span().to(last.span());
struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits")
.span_label(ident_span, "auto trait cannot have super traits")
.span_suggestion(
span,
"remove the super traits",
String::new(),
Applicability::MachineApplicable,
)
.emit();
if let [.., last] = &bounds[..] {
let span = ident_span.shrink_to_hi().to(last.span());
self.emit_e0568(span, ident_span);
}
}

fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
if !where_clause.predicates.is_empty() {
self.emit_e0568(where_clause.span, ident_span);
}
}

fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
if !trait_items.is_empty() {
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
struct_span_err!(
self.session,
spans,
E0380,
"auto traits cannot have methods or associated items"
"auto traits cannot have associated items"
)
.span_suggestion(
total_span,
"remove these associated items",
String::new(),
Applicability::MachineApplicable,
)
.span_label(ident_span, "auto trait cannot have items")
.span_label(ident_span, "auto trait cannot have associated items")
.emit();
}
}
Expand Down Expand Up @@ -1184,6 +1206,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// Auto traits cannot have generics, super traits nor contain items.
self.deny_generic_params(generics, item.ident.span);
self.deny_super_traits(bounds, item.ident.span);
self.deny_where_clause(&generics.where_clause, item.ident.span);
self.deny_items(trait_items, item.ident.span);
}
self.no_questions_in_bounds(bounds, "supertraits", true);
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/auto-traits/auto-trait-validation.fixed
@@ -0,0 +1,13 @@
#![feature(auto_traits)]

// run-rustfix

auto trait Generic {}
//~^ auto traits cannot have generic parameters [E0567]
auto trait Bound {}
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
auto trait LifetimeBound {}
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
auto trait MyTrait { }
//~^ auto traits cannot have associated items [E0380]
fn main() {}
8 changes: 6 additions & 2 deletions src/test/ui/auto-traits/auto-trait-validation.rs
@@ -1,9 +1,13 @@
#![feature(auto_traits)]

// run-rustfix

auto trait Generic<T> {}
//~^ auto traits cannot have generic parameters [E0567]
auto trait Bound : Copy {}
//~^ auto traits cannot have super traits [E0568]
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
auto trait LifetimeBound : 'static {}
//~^ auto traits cannot have super traits or lifetime bounds [E0568]
auto trait MyTrait { fn foo() {} }
//~^ auto traits cannot have methods or associated items [E0380]
//~^ auto traits cannot have associated items [E0380]
fn main() {}
31 changes: 20 additions & 11 deletions src/test/ui/auto-traits/auto-trait-validation.stderr
@@ -1,28 +1,37 @@
error[E0567]: auto traits cannot have generic parameters
--> $DIR/auto-trait-validation.rs:3:19
--> $DIR/auto-trait-validation.rs:5:19
|
LL | auto trait Generic<T> {}
| -------^^^ help: remove the parameters
| |
| auto trait cannot have generic parameters

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

error[E0380]: auto traits cannot have methods or associated items
--> $DIR/auto-trait-validation.rs:7:25
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/auto-trait-validation.rs:9:25
|
LL | auto trait MyTrait { fn foo() {} }
| ------- ^^^
LL | auto trait LifetimeBound : 'static {}
| -------------^^^^^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have items
| auto trait cannot have super traits or lifetime bounds

error[E0380]: auto traits cannot have associated items
--> $DIR/auto-trait-validation.rs:11:25
|
LL | auto trait MyTrait { fn foo() {} }
| ------- ---^^^-----
| | |
| | help: remove these associated items
| auto trait cannot have associated items

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0380, E0567, E0568.
For more information about an error, try `rustc --explain E0380`.
6 changes: 3 additions & 3 deletions src/test/ui/auto-traits/issue-23080-2.stderr
@@ -1,10 +1,10 @@
error[E0380]: auto traits cannot have methods or associated items
error[E0380]: auto traits cannot have associated items
--> $DIR/issue-23080-2.rs:5:10
|
LL | unsafe auto trait Trait {
| ----- auto trait cannot have items
| ----- auto trait cannot have associated items
LL | type Output;
| ^^^^^^
| -----^^^^^^- help: remove these associated items

error: aborting due to previous error

Expand Down
13 changes: 8 additions & 5 deletions src/test/ui/auto-traits/issue-23080.stderr
@@ -1,10 +1,13 @@
error[E0380]: auto traits cannot have methods or associated items
error[E0380]: auto traits cannot have associated items
--> $DIR/issue-23080.rs:5:8
|
LL | unsafe auto trait Trait {
| ----- auto trait cannot have items
LL | fn method(&self) {
| ^^^^^^
LL | unsafe auto trait Trait {
| ----- auto trait cannot have associated items
LL | fn method(&self) {
| _____- ^^^^^^
LL | | println!("Hello");
LL | | }
| |_____- help: remove these associated items

error: aborting due to previous error

Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/auto-traits/issue-84075.rs
@@ -0,0 +1,16 @@
// Regression test for issue #84075.

#![feature(auto_traits)]

auto trait Magic where Self: Copy {} //~ ERROR E0568
impl<T: Magic> Magic for T {}

fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }

#[derive(Debug)]
struct NoClone;

fn main() {
let (a, b) = copy(NoClone);
println!("{:?} {:?}", a, b);
}
11 changes: 11 additions & 0 deletions src/test/ui/auto-traits/issue-84075.stderr
@@ -0,0 +1,11 @@
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/issue-84075.rs:5:18
|
LL | auto trait Magic where Self: Copy {}
| ----- ^^^^^^^^^^^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have super traits or lifetime bounds

error: aborting due to previous error

For more information about this error, try `rustc --explain E0568`.
Expand Up @@ -2,6 +2,7 @@
#![feature(negative_impls)]

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

fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
Expand Down
18 changes: 13 additions & 5 deletions src/test/ui/auto-traits/typeck-auto-trait-no-supertraits-2.stderr
@@ -1,11 +1,19 @@
error[E0568]: auto traits cannot have super traits
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:20
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:17
|
LL | auto trait Magic : Sized where Option<Self> : Magic {}
| ----- ^^^^^ help: remove the super traits
| -----^^^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have super traits
| auto trait cannot have super traits or lifetime bounds

error: aborting due to previous error
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:26
|
LL | auto trait Magic : Sized where Option<Self> : Magic {}
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have super traits or lifetime bounds

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0568`.
@@ -1,10 +1,10 @@
error[E0568]: auto traits cannot have super traits
--> $DIR/typeck-auto-trait-no-supertraits.rs:28:19
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/typeck-auto-trait-no-supertraits.rs:28:17
|
LL | auto trait Magic: Copy {}
| ----- ^^^^ help: remove the super traits
| -----^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have super traits
| auto trait cannot have super traits or lifetime bounds

error: aborting due to previous error

Expand Down
@@ -1,10 +1,10 @@
error[E0568]: auto traits cannot have super traits
--> $DIR/supertrait-auto-trait.rs:8:19
error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/supertrait-auto-trait.rs:8:17
|
LL | auto trait Magic: Copy {}
| ----- ^^^^ help: remove the super traits
| -----^^^^^^ help: remove the super traits or lifetime bounds
| |
| auto trait cannot have super traits
| auto trait cannot have super traits or lifetime bounds

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

0 comments on commit e34fd54

Please sign in to comment.