Skip to content

Commit edd52b1

Browse files
leoyvenspetrochenkov
authored andcommitted
Remove wfcheck for auto traits, remove dead error codes
The WF checks are now done as an AST validation.
1 parent 02b5fee commit edd52b1

File tree

9 files changed

+23
-111
lines changed

9 files changed

+23
-111
lines changed

src/librustc_passes/ast_validation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
247247
if is_auto == IsAuto::Yes {
248248
// Auto traits cannot have generics, super traits nor contain items.
249249
if generics.is_parameterized() {
250-
self.err_handler().span_err(item.span,
251-
"auto traits cannot have generics");
250+
struct_span_err!(self.session, item.span, E0567,
251+
"Auto traits cannot have generic parameters").emit();
252252
}
253253
if !bounds.is_empty() {
254-
self.err_handler().span_err(item.span,
255-
"auto traits cannot have super traits");
254+
struct_span_err!(self.session, item.span, E0568,
255+
"Auto traits cannot have predicates").emit();
256256
}
257257
if !trait_items.is_empty() {
258-
self.err_handler().span_err(item.span,
259-
"auto traits cannot contain items");
258+
struct_span_err!(self.session, item.span, E0380,
259+
"Auto traits cannot have methods or associated items").emit();
260260
}
261261
}
262262
self.no_questions_in_bounds(bounds, "supertraits", true);

src/librustc_passes/diagnostics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ Trait methods cannot be declared `const` by design. For more information, see
150150
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
151151
"##,
152152

153+
E0380: r##"
154+
Auto traits cannot have methods or associated items.
155+
For more information see the [opt-in builtin traits RFC][RFC 19].
156+
157+
[RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
158+
"##,
159+
153160
E0449: r##"
154161
A visibility qualifier was used when it was unnecessary. Erroneous code
155162
examples:
@@ -264,5 +271,7 @@ register_diagnostics! {
264271
E0226, // only a single explicit lifetime bound is permitted
265272
E0472, // asm! is unsupported on this target
266273
E0561, // patterns aren't allowed in function pointer types
274+
E0567, // auto traits can not have type parameters
275+
E0568, // auto traits can not have predicates
267276
E0642, // patterns aren't allowed in methods without bodies
268277
}

src/librustc_typeck/check/wfcheck.rs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -275,74 +275,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
275275
});
276276
}
277277

278-
fn check_auto_trait(&mut self, trait_def_id: DefId, span: Span) {
279-
// We want to ensure:
280-
//
281-
// 1) that there are no items contained within
282-
// the trait definition
283-
//
284-
// 2) that the definition doesn't violate the no-super trait rule
285-
// for auto traits.
286-
//
287-
// 3) that the trait definition does not have any type parameters
288-
289-
let predicates = self.tcx.predicates_of(trait_def_id);
290-
291-
// We must exclude the Self : Trait predicate contained by all
292-
// traits.
293-
let has_predicates =
294-
predicates.predicates.iter().any(|predicate| {
295-
match predicate {
296-
&ty::Predicate::Trait(ref poly_trait_ref) => {
297-
let self_ty = poly_trait_ref.0.self_ty();
298-
!(self_ty.is_self() && poly_trait_ref.def_id() == trait_def_id)
299-
},
300-
_ => true,
301-
}
302-
});
303-
304-
let has_ty_params = self.tcx.generics_of(trait_def_id).types.len() > 1;
305-
306-
// We use an if-else here, since the generics will also trigger
307-
// an extraneous error message when we find predicates like
308-
// `T : Sized` for a trait like: `trait Magic<T>`.
309-
//
310-
// We also put the check on the number of items here,
311-
// as it seems confusing to report an error about
312-
// extraneous predicates created by things like
313-
// an associated type inside the trait.
314-
let mut err = None;
315-
if !self.tcx.associated_item_def_ids(trait_def_id).is_empty() {
316-
error_380(self.tcx, span);
317-
} else if has_ty_params {
318-
err = Some(struct_span_err!(self.tcx.sess, span, E0567,
319-
"traits with auto impls (`e.g. impl \
320-
Trait for ..`) can not have type parameters"));
321-
} else if has_predicates {
322-
err = Some(struct_span_err!(self.tcx.sess, span, E0568,
323-
"traits with auto impls (`e.g. impl \
324-
Trait for ..`) cannot have predicates"));
325-
}
326-
327-
// Finally if either of the above conditions apply we should add a note
328-
// indicating that this error is the result of a recent soundness fix.
329-
match err {
330-
None => {},
331-
Some(mut e) => {
332-
e.note("the new auto trait rules are the result of a \
333-
recent soundness fix; see #29859 for more details");
334-
e.emit();
335-
}
336-
}
337-
}
338-
339278
fn check_trait(&mut self, item: &hir::Item) {
340279
let trait_def_id = self.tcx.hir.local_def_id(item.id);
341-
342-
if self.tcx.trait_is_auto(trait_def_id) {
343-
self.check_auto_trait(trait_def_id, item.span);
344-
}
345-
346280
self.for_item(item).with_fcx(|fcx, this| {
347281
let predicates = fcx.tcx.predicates_of(trait_def_id).instantiate_identity(fcx.tcx);
348282
let predicates = fcx.normalize_associated_types_in(item.span, &predicates);
@@ -733,12 +667,6 @@ fn error_192(tcx: TyCtxt, span: Span) {
733667
default impls (e.g., `Send` and `Sync`)")
734668
}
735669

736-
fn error_380(tcx: TyCtxt, span: Span) {
737-
span_err!(tcx.sess, span, E0380,
738-
"traits with default impls (`e.g. impl \
739-
Trait for ..`) must have no methods or associated items")
740-
}
741-
742670
fn error_392<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span: Span, param_name: ast::Name)
743671
-> DiagnosticBuilder<'tcx> {
744672
let mut err = struct_span_err!(tcx.sess, span, E0392,

src/librustc_typeck/collect.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,9 @@ fn trait_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
712712
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
713713
let item = tcx.hir.expect_item(node_id);
714714

715-
let unsafety = match item.node {
716-
hir::ItemTrait(_, unsafety, ..) => unsafety,
717-
hir::ItemTraitAlias(..) => hir::Unsafety::Normal,
715+
let (is_auto, unsafety) = match item.node {
716+
hir::ItemTrait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety),
717+
hir::ItemTraitAlias(..) => (hir::IsAuto::No, hir::Unsafety::Normal),
718718
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
719719
};
720720

@@ -731,10 +731,6 @@ fn trait_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
731731
}
732732

733733
let def_path_hash = tcx.def_path_hash(def_id);
734-
let is_auto = match item.node {
735-
hir::ItemTrait(hir::IsAuto::Yes, ..) => true,
736-
_ => tcx.hir.trait_is_auto(def_id),
737-
};
738734
let def = ty::TraitDef::new(def_id,
739735
unsafety,
740736
paren_sugar,

src/librustc_typeck/diagnostics.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,13 +2531,6 @@ struct Bar<S, T> { x: Foo<S, T> }
25312531
```
25322532
"##,
25332533

2534-
E0318: r##"
2535-
Default impls for a trait must be located in the same crate where the trait was
2536-
defined. For more information see the [opt-in builtin traits RFC][RFC 19].
2537-
2538-
[RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
2539-
"##,
2540-
25412534
E0321: r##"
25422535
A cross-crate opt-out trait was implemented on something which wasn't a struct
25432536
or enum type. Erroneous code example:
@@ -3170,13 +3163,6 @@ containing the unsized type is the last and only unsized type field in the
31703163
struct.
31713164
"##,
31723165

3173-
E0380: r##"
3174-
Default impls are only allowed for traits with no methods or associated items.
3175-
For more information see the [opt-in builtin traits RFC][RFC 19].
3176-
3177-
[RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
3178-
"##,
3179-
31803166
E0390: r##"
31813167
You tried to implement methods for a primitive type. Erroneous code example:
31823168
@@ -4729,13 +4715,10 @@ register_diagnostics! {
47294715
// E0372, // coherence not object safe
47304716
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
47314717
// between structures with the same definition
4732-
E0521, // redundant auto implementations of trait
47334718
E0533, // `{}` does not name a unit variant, unit struct or a constant
47344719
// E0563, // cannot determine a type for this `impl Trait`: {} // removed in 6383de15
47354720
E0564, // only named lifetimes are allowed in `impl Trait`,
47364721
// but `{}` was found in the type `{}`
4737-
E0567, // auto traits can not have type parameters
4738-
E0568, // auto-traits can not have predicates,
47394722
E0587, // struct has conflicting packed and align representation hints
47404723
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
47414724
E0592, // duplicate definitions with name `{}`

src/test/compile-fail/auto-trait-validation.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
#![feature(optin_builtin_traits)]
1212

1313
auto trait Generic<T> {}
14-
//~^ ERROR auto traits cannot have generics
15-
//~^^ traits with auto impls (`e.g. impl Trait for ..`) can not have type parameters
14+
//~^ Auto traits cannot have type parameters [E0567]
1615
auto trait Bound : Copy {}
17-
//~^ ERROR auto traits cannot have super traits
18-
//~^^ traits with auto impls (`e.g. impl Trait for ..`) cannot have predicates
16+
//~^ Auto traits cannot have predicates [E0568]
1917
auto trait MyTrait { fn foo() {} }
20-
//~^ ERROR auto traits cannot contain items
21-
//~^^ traits with default impls (`e.g. impl Trait for ..`) must have no methods or associated items
18+
//~^ Auto traits cannot have methods or associated items [E0380]
2219
fn main() {}

src/test/compile-fail/coherence-default-trait-impl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ auto trait MySafeTrait {}
1414

1515
struct Foo;
1616

17-
#[allow(auto_impl)]
1817
unsafe impl MySafeTrait for Foo {}
1918
//~^ ERROR implementing the trait `MySafeTrait` is not unsafe
2019

2120
unsafe auto trait MyUnsafeTrait {}
2221

23-
#[allow(auto_impl)]
2422
impl MyUnsafeTrait for Foo {}
2523
//~^ ERROR the trait `MyUnsafeTrait` requires an `unsafe impl` declaration
2624

src/test/compile-fail/issue-23080-2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ fn call_method<T: Trait>(x: T) {}
2222
fn main() {
2323
// ICE
2424
call_method(());
25+
//~^ ERROR
2526
}

src/test/compile-fail/traits-inductive-overflow-supertrait-oibit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
2222
struct NoClone;
2323

2424
fn main() {
25-
let (a, b) = copy(NoClone);
25+
let (a, b) = copy(NoClone); //~ ERROR
2626
println!("{:?} {:?}", a, b);
2727
}

0 commit comments

Comments
 (0)