1
Fork 0

Deny where clauses on auto traits

This commit is contained in:
Fabian Wolff 2021-10-03 17:58:10 +02:00
parent edebf77e00
commit e34fd54611
12 changed files with 135 additions and 47 deletions

View file

@ -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) { fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
if let [first @ last] | [first, .., last] = &bounds[..] { if let [.., last] = &bounds[..] {
let span = first.span().to(last.span()); let span = ident_span.shrink_to_hi().to(last.span());
struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits") self.emit_e0568(span, ident_span);
.span_label(ident_span, "auto trait cannot have super traits") }
.span_suggestion( }
span,
"remove the super traits", fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
String::new(), if !where_clause.predicates.is_empty() {
Applicability::MachineApplicable, self.emit_e0568(where_clause.span, ident_span);
)
.emit();
} }
} }
fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) { fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
if !trait_items.is_empty() { if !trait_items.is_empty() {
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect(); 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!( struct_span_err!(
self.session, self.session,
spans, spans,
E0380, E0380,
"auto traits cannot have methods or associated items" "auto traits cannot have associated items"
) )
.span_label(ident_span, "auto trait cannot have items") .span_suggestion(
total_span,
"remove these associated items",
String::new(),
Applicability::MachineApplicable,
)
.span_label(ident_span, "auto trait cannot have associated items")
.emit(); .emit();
} }
} }
@ -1184,6 +1206,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// Auto traits cannot have generics, super traits nor contain items. // Auto traits cannot have generics, super traits nor contain items.
self.deny_generic_params(generics, item.ident.span); self.deny_generic_params(generics, item.ident.span);
self.deny_super_traits(bounds, 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.deny_items(trait_items, item.ident.span);
} }
self.no_questions_in_bounds(bounds, "supertraits", true); self.no_questions_in_bounds(bounds, "supertraits", true);

View file

@ -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() {}

View file

@ -1,9 +1,13 @@
#![feature(auto_traits)] #![feature(auto_traits)]
// run-rustfix
auto trait Generic<T> {} auto trait Generic<T> {}
//~^ auto traits cannot have generic parameters [E0567] //~^ auto traits cannot have generic parameters [E0567]
auto trait Bound : Copy {} 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 trait MyTrait { fn foo() {} }
//~^ auto traits cannot have methods or associated items [E0380] //~^ auto traits cannot have associated items [E0380]
fn main() {} fn main() {}

View file

@ -1,28 +1,37 @@
error[E0567]: auto traits cannot have generic parameters 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> {} LL | auto trait Generic<T> {}
| -------^^^ help: remove the parameters | -------^^^ help: remove the parameters
| | | |
| auto trait cannot have generic parameters | auto trait cannot have generic parameters
error[E0568]: auto traits cannot have super traits error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/auto-trait-validation.rs:5:20 --> $DIR/auto-trait-validation.rs:7:17
| |
LL | auto trait Bound : Copy {} 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 error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/auto-trait-validation.rs:7:25 --> $DIR/auto-trait-validation.rs:9:25
|
LL | auto trait LifetimeBound : 'static {}
| -------------^^^^^^^^^^ help: remove the super traits or lifetime bounds
| |
| 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() {} } LL | auto trait MyTrait { fn foo() {} }
| ------- ^^^ | ------- ---^^^-----
| | | | |
| auto trait cannot have items | | 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. Some errors have detailed explanations: E0380, E0567, E0568.
For more information about an error, try `rustc --explain E0380`. For more information about an error, try `rustc --explain E0380`.

View file

@ -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 --> $DIR/issue-23080-2.rs:5:10
| |
LL | unsafe auto trait Trait { LL | unsafe auto trait Trait {
| ----- auto trait cannot have items | ----- auto trait cannot have associated items
LL | type Output; LL | type Output;
| ^^^^^^ | -----^^^^^^- help: remove these associated items
error: aborting due to previous error error: aborting due to previous error

View file

@ -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 --> $DIR/issue-23080.rs:5:8
| |
LL | unsafe auto trait Trait { LL | unsafe auto trait Trait {
| ----- auto trait cannot have items | ----- auto trait cannot have associated items
LL | fn method(&self) { LL | fn method(&self) {
| ^^^^^^ | _____- ^^^^^^
LL | | println!("Hello");
LL | | }
| |_____- help: remove these associated items
error: aborting due to previous error error: aborting due to previous error

View file

@ -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);
}

View file

@ -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`.

View file

@ -2,6 +2,7 @@
#![feature(negative_impls)] #![feature(negative_impls)]
auto trait Magic : Sized where Option<Self> : Magic {} //~ ERROR E0568 auto trait Magic : Sized where Option<Self> : Magic {} //~ ERROR E0568
//~^ ERROR E0568
impl<T:Magic> Magic for T {} impl<T:Magic> Magic for T {}
fn copy<T: Magic>(x: T) -> (T, T) { (x, x) } fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }

View file

@ -1,11 +1,19 @@
error[E0568]: auto traits cannot have super traits error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:20 --> $DIR/typeck-auto-trait-no-supertraits-2.rs:4:17
| |
LL | auto trait Magic : Sized where Option<Self> : Magic {} 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`. For more information about this error, try `rustc --explain E0568`.

View file

@ -1,10 +1,10 @@
error[E0568]: auto traits cannot have super traits error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/typeck-auto-trait-no-supertraits.rs:28:19 --> $DIR/typeck-auto-trait-no-supertraits.rs:28:17
| |
LL | auto trait Magic: Copy {} 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 error: aborting due to previous error

View file

@ -1,10 +1,10 @@
error[E0568]: auto traits cannot have super traits error[E0568]: auto traits cannot have super traits or lifetime bounds
--> $DIR/supertrait-auto-trait.rs:8:19 --> $DIR/supertrait-auto-trait.rs:8:17
| |
LL | auto trait Magic: Copy {} 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 error[E0277]: the trait bound `NoClone: Copy` is not satisfied
--> $DIR/supertrait-auto-trait.rs:16:23 --> $DIR/supertrait-auto-trait.rs:16:23