Skip to content

Commit ba9b196

Browse files
committed
Autotrait bounds on dyn-safe trait methods
1 parent a247952 commit ba9b196

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+22
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ fn do_orphan_check_impl<'tcx>(
116116
// impl MyAuto for dyn Trait {} // NOT OKAY
117117
// impl<T: ?Sized> MyAuto for T {} // NOT OKAY
118118
//
119+
// With this restriction, it's guaranteed that an auto-trait is
120+
// implemented for a trait object if and only if the auto-trait is
121+
// one of the trait object's trait bounds (or a supertrait of a
122+
// bound). In other words `dyn Trait + AutoTrait` always implements
123+
// AutoTrait, while `dyn Trait` never implements AutoTrait.
124+
//
125+
// This is necessary in order for autotrait bounds on methods of
126+
// trait objects to be sound.
127+
//
128+
// auto trait AutoTrait {}
129+
//
130+
// trait ObjectSafeTrait {
131+
// fn f(&self) where Self: AutoTrait;
132+
// }
133+
//
134+
// We can allow f to be called on `dyn ObjectSafeTrait + AutoTrait`.
135+
//
136+
// If we didn't deny `impl AutoTrait for dyn Trait`, it would be
137+
// unsound for the ObjectSafeTrait shown above to be object safe
138+
// because someone could take some type implementing ObjectSafeTrait
139+
// but not AutoTrait, unsize it to `dyn ObjectSafeTrait`, and call
140+
// .f() which has no concrete implementation (issue #50781).
119141
let problematic_kind = match self_ty_kind {
120142
ty::Dynamic(..) => Some("trait object"),
121143
ty::Param(..) if !self_ty.is_sized(tcx, tcx.param_env(def_id)) => {

compiler/rustc_trait_selection/src/traits/object_safety.rs

+42-10
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,48 @@ fn virtual_call_violation_for_method<'tcx>(
529529

530530
// NOTE: This check happens last, because it results in a lint, and not a
531531
// hard error.
532-
if tcx
533-
.predicates_of(method.def_id)
534-
.predicates
535-
.iter()
536-
// A trait object can't claim to live more than the concrete type,
537-
// so outlives predicates will always hold.
538-
.cloned()
539-
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
540-
.any(|pred| contains_illegal_self_type_reference(tcx, trait_def_id, pred))
541-
{
532+
if tcx.predicates_of(method.def_id).predicates.iter().any(|(pred, _span)| {
533+
// dyn Trait is okay:
534+
//
535+
// trait Trait {
536+
// fn f(&self) where Self: 'static;
537+
// }
538+
//
539+
// because a trait object can't claim to live longer than the concrete
540+
// type. If the lifetime bound holds on dyn Trait then it's guaranteed
541+
// to hold as well on the concrete type.
542+
if pred.to_opt_type_outlives().is_some() {
543+
return false;
544+
}
545+
546+
// dyn Trait is okay:
547+
//
548+
// auto trait AutoTrait {}
549+
//
550+
// trait Trait {
551+
// fn f(&self) where Self: AutoTrait;
552+
// }
553+
//
554+
// because `impl AutoTrait for dyn Trait` is disallowed by coherence.
555+
// Traits with a default impl are implemented for a trait object if and
556+
// only if the autotrait is one of the trait object's trait bounds, like
557+
// in `dyn Trait + AutoTrait`.
558+
if let ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
559+
trait_ref: pred_trait_ref,
560+
constness: ty::BoundConstness::NotConst,
561+
polarity: ty::ImplPolarity::Positive,
562+
})) = pred.kind().skip_binder()
563+
&& pred_trait_ref.self_ty() == tcx.types.self_param
564+
&& tcx.trait_is_auto(pred_trait_ref.def_id)
565+
{
566+
// Only check the rest of the bound's parameters. So `Self: Bound<Self>`
567+
// is still considered illegal.
568+
let rest_of_substs = &pred_trait_ref.substs[1..];
569+
return contains_illegal_self_type_reference(tcx, trait_def_id, rest_of_substs);
570+
}
571+
572+
contains_illegal_self_type_reference(tcx, trait_def_id, pred.clone())
573+
}) {
542574
return Some(MethodViolationCode::WhereClauseReferencesSelf);
543575
}
544576

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-fail
2+
3+
#![feature(auto_traits)]
4+
#![deny(where_clauses_object_safety)]
5+
6+
auto trait AutoTrait {}
7+
8+
trait Trait {
9+
fn static_lifetime_bound(&self) where Self: 'static {}
10+
11+
fn arg_lifetime_bound<'a>(&self, _arg: &'a ()) where Self: 'a {}
12+
13+
fn autotrait_bound(&self) where Self: AutoTrait {}
14+
}
15+
16+
impl Trait for () {}
17+
18+
fn main() {
19+
let trait_object = &() as &dyn Trait;
20+
trait_object.static_lifetime_bound();
21+
trait_object.arg_lifetime_bound(&());
22+
trait_object.autotrait_bound(); //~ ERROR: the trait bound `dyn Trait: AutoTrait` is not satisfied
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `dyn Trait: AutoTrait` is not satisfied
2+
--> $DIR/self-in-where-clause-allowed.rs:22:18
3+
|
4+
LL | trait_object.autotrait_bound();
5+
| ^^^^^^^^^^^^^^^ the trait `AutoTrait` is not implemented for `dyn Trait`
6+
|
7+
note: required by a bound in `Trait::autotrait_bound`
8+
--> $DIR/self-in-where-clause-allowed.rs:13:43
9+
|
10+
LL | fn autotrait_bound(&self) where Self: AutoTrait {}
11+
| ^^^^^^^^^ required by this bound in `Trait::autotrait_bound`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)