Skip to content

Commit f46f388

Browse files
committed
Fix checking of auto trait bounds in trait objects.
Any auto trait is allowed in trait object bounds. Fix duplicate check of type and lifetime parameter count.
1 parent d762b1d commit f46f388

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

src/librustc_typeck/astconv.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
572572
let b = &trait_bounds[0];
573573
let span = b.trait_ref.path.span;
574574
struct_span_err!(self.tcx().sess, span, E0225,
575-
"only Send/Sync traits can be used as additional traits in a trait object")
576-
.span_label(span, "non-Send/Sync additional trait")
575+
"only auto traits can be used as additional traits in a trait object")
576+
.span_label(span, "non-auto additional trait")
577577
.emit();
578578
}
579579

@@ -1311,27 +1311,10 @@ fn split_auto_traits<'a, 'b, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
13111311
-> (Vec<DefId>, Vec<&'b hir::PolyTraitRef>)
13121312
{
13131313
let (auto_traits, trait_bounds): (Vec<_>, _) = trait_bounds.iter().partition(|bound| {
1314+
// Checks whether `trait_did` is an auto trait and adds it to `auto_traits` if so.
13141315
match bound.trait_ref.path.def {
1315-
Def::Trait(trait_did) => {
1316-
// Checks whether `trait_did` refers to one of the builtin
1317-
// traits, like `Send`, and adds it to `auto_traits` if so.
1318-
if Some(trait_did) == tcx.lang_items().send_trait() ||
1319-
Some(trait_did) == tcx.lang_items().sync_trait() {
1320-
let segments = &bound.trait_ref.path.segments;
1321-
segments[segments.len() - 1].with_parameters(|parameters| {
1322-
if !parameters.types.is_empty() {
1323-
check_type_argument_count(tcx, bound.trait_ref.path.span,
1324-
parameters.types.len(), &[]);
1325-
}
1326-
if !parameters.lifetimes.is_empty() {
1327-
report_lifetime_number_error(tcx, bound.trait_ref.path.span,
1328-
parameters.lifetimes.len(), 0);
1329-
}
1330-
});
1331-
true
1332-
} else {
1333-
false
1334-
}
1316+
Def::Trait(trait_did) if tcx.trait_is_auto(trait_did) => {
1317+
true
13351318
}
13361319
_ => false
13371320
}

src/librustc_typeck/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2455,9 +2455,9 @@ fn main() {
24552455
}
24562456
```
24572457
2458-
Send and Sync are an exception to this rule: it's possible to have bounds of
2459-
one non-builtin trait, plus either or both of Send and Sync. For example, the
2460-
following compiles correctly:
2458+
Auto traits such as Send and Sync are an exception to this rule:
2459+
It's possible to have bounds of one non-builtin trait, plus any number of
2460+
auto traits. For example, the following compiles correctly:
24612461
24622462
```
24632463
fn main() {

src/test/compile-fail/E0225.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
fn main() {
1212
let _: Box<std::io::Read + std::io::Write>;
13-
//~^ ERROR only Send/Sync traits can be used as additional traits in a trait object [E0225]
14-
//~| NOTE non-Send/Sync additional trait
13+
//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
14+
//~| NOTE non-auto additional trait
1515
}

src/test/compile-fail/bad-sized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Trait {}
1212

1313
pub fn main() {
1414
let x: Vec<Trait + Sized> = Vec::new();
15-
//~^ ERROR only Send/Sync traits can be used as additional traits in a trait object
15+
//~^ ERROR only auto traits can be used as additional traits in a trait object
1616
//~| ERROR the trait bound `Trait: std::marker::Sized` is not satisfied
1717
//~| ERROR the trait bound `Trait: std::marker::Sized` is not satisfied
1818
}

src/test/compile-fail/issue-22560.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ type Test = Add +
2323
//~| NOTE missing reference to `RHS`
2424
//~| NOTE because of the default `Self` reference, type parameters must be specified on object types
2525
//~| ERROR E0225
26-
//~| NOTE non-Send/Sync additional trait
26+
//~| NOTE non-auto additional trait
2727

2828
fn main() { }

src/test/compile-fail/issue-32963.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
1616

1717
fn main() {
1818
size_of_copy::<Misc+Copy>();
19-
//~^ ERROR only Send/Sync traits can be used as additional traits in a trait object
19+
//~^ ERROR only auto traits can be used as additional traits in a trait object
2020
//~| ERROR the trait bound `Misc: std::marker::Copy` is not satisfied
2121
}

src/test/run-pass/auto-traits.rs

+3
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ fn main() {
3333
take_auto(AutoBool(true));
3434
take_auto_unsafe(0);
3535
take_auto_unsafe(AutoBool(true));
36+
37+
/// Auto traits are allowed in trait object bounds.
38+
let _: &(Send + Auto) = &0;
3639
}

0 commit comments

Comments
 (0)