Skip to content

Commit 2bb97c0

Browse files
committed
Default auto traits: revert to the default supertraits
1 parent 8365fcb commit 2bb97c0

File tree

8 files changed

+146
-326
lines changed

8 files changed

+146
-326
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
174174
}
175175
};
176176

177-
if let Node::TraitItem(item) = node {
178-
let mut bounds = Vec::new();
179-
icx.lowerer().add_default_trait_item_bounds(item, &mut bounds);
180-
predicates.extend(bounds);
181-
}
182-
183177
let generics = tcx.generics_of(def_id);
184178

185179
// Below we'll consider the bounds on the type parameters (including `Self`)

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 13 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use std::assert_matches::assert_matches;
12
use std::ops::ControlFlow;
23

34
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
45
use rustc_errors::codes::*;
56
use rustc_errors::struct_span_code_err;
67
use rustc_hir as hir;
8+
use rustc_hir::PolyTraitRef;
79
use rustc_hir::def::{DefKind, Res};
810
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
9-
use rustc_hir::{AmbigArg, PolyTraitRef};
1011
use rustc_middle::bug;
1112
use rustc_middle::ty::{
1213
self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@@ -230,122 +231,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
230231
}
231232
}
232233

233-
/// Checks whether `Self: DefaultAutoTrait` bounds should be added on trait super bounds
234-
/// or associated items.
235-
///
236-
/// To keep backward compatibility with existing code, `experimental_default_bounds` bounds
237-
/// should be added everywhere, including super bounds. However this causes a huge performance
238-
/// costs. For optimization purposes instead of adding default supertraits, bounds
239-
/// are added to the associated items:
240-
///
241-
/// ```ignore(illustrative)
242-
/// // Default bounds are generated in the following way:
243-
/// trait Trait {
244-
/// fn foo(&self) where Self: Leak {}
245-
/// }
246-
///
247-
/// // instead of this:
248-
/// trait Trait: Leak {
249-
/// fn foo(&self) {}
250-
/// }
251-
/// ```
252-
/// It is not always possible to do this because of backward compatibility:
253-
///
254-
/// ```ignore(illustrative)
255-
/// pub trait Trait<Rhs = Self> {}
256-
/// pub trait Trait1 : Trait {}
257-
/// //~^ ERROR: `Rhs` requires `DefaultAutoTrait`, but `Self` is not `DefaultAutoTrait`
258-
/// ```
259-
///
260-
/// or:
261-
///
262-
/// ```ignore(illustrative)
263-
/// trait Trait {
264-
/// type Type where Self: Sized;
265-
/// }
266-
/// trait Trait2<T> : Trait<Type = T> {}
267-
/// //~^ ERROR: `DefaultAutoTrait` required for `Trait2`, by implicit `Self: DefaultAutoTrait` in `Trait::Type`
268-
/// ```
269-
///
270-
/// Therefore, `experimental_default_bounds` are still being added to supertraits if
271-
/// the `SelfTyParam` or `AssocItemConstraint` were found in a trait header.
272-
fn requires_default_supertraits(
273-
&self,
274-
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
275-
hir_generics: &'tcx hir::Generics<'tcx>,
276-
) -> bool {
277-
struct TraitInfoCollector;
278-
279-
impl<'tcx> hir::intravisit::Visitor<'tcx> for TraitInfoCollector {
280-
type Result = ControlFlow<()>;
281-
282-
fn visit_assoc_item_constraint(
283-
&mut self,
284-
_constraint: &'tcx hir::AssocItemConstraint<'tcx>,
285-
) -> Self::Result {
286-
ControlFlow::Break(())
287-
}
288-
289-
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) -> Self::Result {
290-
if matches!(
291-
&t.kind,
292-
hir::TyKind::Path(hir::QPath::Resolved(
293-
_,
294-
hir::Path { res: hir::def::Res::SelfTyParam { .. }, .. },
295-
))
296-
) {
297-
return ControlFlow::Break(());
298-
}
299-
hir::intravisit::walk_ty(self, t)
300-
}
301-
}
302-
303-
let mut found = false;
304-
for bound in hir_bounds {
305-
found |= hir::intravisit::walk_param_bound(&mut TraitInfoCollector, bound).is_break();
306-
}
307-
found |= hir::intravisit::walk_generics(&mut TraitInfoCollector, hir_generics).is_break();
308-
found
309-
}
310-
311-
/// Implicitly add `Self: DefaultAutoTrait` clauses on trait associated items if
312-
/// they are not added as super trait bounds to the trait itself. See
313-
/// `requires_default_supertraits` for more information.
314-
pub(crate) fn add_default_trait_item_bounds(
315-
&self,
316-
trait_item: &hir::TraitItem<'tcx>,
317-
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
318-
) {
319-
let tcx = self.tcx();
320-
if !tcx.sess.opts.unstable_opts.experimental_default_bounds {
321-
return;
322-
}
323-
324-
let parent = tcx.local_parent(trait_item.hir_id().owner.def_id);
325-
let hir::Node::Item(parent_trait) = tcx.hir_node_by_def_id(parent) else {
326-
unreachable!();
327-
};
328-
329-
let (trait_generics, trait_bounds) = match parent_trait.kind {
330-
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => (generics, supertraits),
331-
hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
332-
_ => unreachable!(),
333-
};
334-
335-
if !self.requires_default_supertraits(trait_bounds, trait_generics) {
336-
let self_ty_where_predicates = (parent, trait_item.generics.predicates);
337-
self.add_default_traits(
338-
bounds,
339-
tcx.types.self_param,
340-
&[],
341-
Some(self_ty_where_predicates),
342-
trait_item.span,
343-
);
344-
}
345-
}
346-
347-
/// Lazily sets `experimental_default_bounds` to true on trait super bounds.
348-
/// See `requires_default_supertraits` for more information.
234+
/// Sets `experimental_default_bounds` to true on trait super bounds.
349235
pub(crate) fn add_default_super_traits(
350236
&self,
351237
trait_def_id: LocalDefId,
@@ -354,21 +240,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
354240
hir_generics: &'tcx hir::Generics<'tcx>,
355241
span: Span,
356242
) {
357-
if !self.tcx().sess.opts.unstable_opts.experimental_default_bounds {
243+
assert_matches!(self.tcx().def_kind(trait_def_id), DefKind::Trait | DefKind::TraitAlias);
244+
245+
if self.tcx().trait_is_auto(trait_def_id.to_def_id()) {
358246
return;
359247
}
360248

361-
assert!(matches!(self.tcx().def_kind(trait_def_id), DefKind::Trait | DefKind::TraitAlias));
362-
if self.requires_default_supertraits(hir_bounds, hir_generics) {
363-
let self_ty_where_predicates = (trait_def_id, hir_generics.predicates);
364-
self.add_default_traits(
365-
bounds,
366-
self.tcx().types.self_param,
367-
hir_bounds,
368-
Some(self_ty_where_predicates),
369-
span,
370-
);
371-
}
249+
self.add_default_traits(
250+
bounds,
251+
self.tcx().types.self_param,
252+
hir_bounds,
253+
Some((trait_def_id, hir_generics.predicates)),
254+
span,
255+
);
372256
}
373257

374258
pub(crate) fn add_default_traits(

compiler/rustc_lint/src/multiple_supertrait_upcastable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
4747
.explicit_super_predicates_of(def_id)
4848
.iter_identity_copied()
4949
.filter_map(|(pred, _)| pred.as_trait_clause())
50-
.filter(|pred| !cx.tcx.is_lang_item(pred.def_id(), hir::LangItem::MetaSized));
50+
.filter(|pred| !cx.tcx.is_lang_item(pred.def_id(), hir::LangItem::MetaSized))
51+
.filter(|pred| !cx.tcx.is_default_trait(pred.def_id()));
5152
if direct_super_traits_iter.count() > 1 {
5253
cx.emit_span_lint(
5354
MULTIPLE_SUPERTRAIT_UPCASTABLE,

tests/ui/traits/default_auto_traits/backward-compatible-lazy-bounds-pass.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/ui/traits/default_auto_traits/maybe-bounds-in-dyn-traits.rs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,37 @@
1313
#![no_core]
1414

1515
#[lang = "pointee_sized"]
16-
trait PointeeSized {}
16+
trait PointeeSized: ?Leak {}
1717

1818
#[lang = "meta_sized"]
19-
trait MetaSized: PointeeSized {}
19+
trait MetaSized: PointeeSized + ?Leak {}
2020

2121
#[lang = "sized"]
22-
trait Sized: MetaSized {}
22+
trait Sized: MetaSized + ?Leak {}
2323

2424
#[lang = "copy"]
25-
pub trait Copy {}
25+
pub trait Copy: ?Leak {}
2626
impl<'a, T: ?Sized> Copy for &'a T {}
2727

2828
#[lang = "legacy_receiver"]
29-
trait Receiver {}
29+
trait Receiver: ?Leak {}
3030
impl<T: ?Sized + ?Leak> Receiver for &T {}
31+
impl<T: ?Sized + ?Leak> Receiver for &mut T {}
3132

3233
#[lang = "unsize"]
33-
trait Unsize<T: ?Sized + ?Leak> {}
34+
trait Unsize<T: ?Sized + ?Leak>: ?Leak {}
3435

3536
#[lang = "coerce_unsized"]
36-
trait CoerceUnsized<T: ?Leak + ?Sized> {}
37+
trait CoerceUnsized<T: ?Leak + ?Sized>: ?Leak {}
3738
impl<'a, 'b: 'a, T: ?Sized + ?Leak + Unsize<U>, U: ?Sized + ?Leak> CoerceUnsized<&'a U> for &'b T {}
39+
// Omit `T: ?Leak` and `U: ?Leak`.
40+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'b mut T {}
3841

3942
#[lang = "dispatch_from_dyn"]
40-
trait DispatchFromDyn<T: ?Leak> {}
43+
trait DispatchFromDyn<T: ?Leak>: ?Leak {}
4144
impl<'a, T: ?Sized + ?Leak + Unsize<U>, U: ?Sized + ?Leak> DispatchFromDyn<&'a U> for &'a T {}
45+
// Omit `T: ?Leak` and `U: ?Leak`.
46+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
4247

4348
#[lang = "default_trait1"]
4449
auto trait Leak {}
@@ -47,25 +52,52 @@ struct NonLeakS;
4752
impl !Leak for NonLeakS {}
4853
struct LeakS;
4954

50-
trait Trait {
51-
fn leak_foo(&self) {}
52-
fn maybe_leak_foo(&self) where Self: ?Leak {}
55+
fn bounds_check() {
56+
trait LeakTr {}
57+
58+
trait MaybeLeakTr: ?Leak {}
59+
60+
impl MaybeLeakTr for NonLeakS {}
61+
62+
impl LeakTr for LeakS {}
63+
impl MaybeLeakTr for LeakS {}
64+
65+
let _: &dyn LeakTr = &NonLeakS;
66+
//~^ ERROR the trait bound `NonLeakS: bounds_check::LeakTr` is not satisfied
67+
let _: &dyn LeakTr = &LeakS;
68+
69+
let _: &(dyn LeakTr + ?Leak) = &NonLeakS;
70+
let _: &(dyn LeakTr + ?Leak) = &LeakS;
71+
72+
let _: &dyn MaybeLeakTr = &NonLeakS;
73+
let _: &dyn MaybeLeakTr = &LeakS;
5374
}
5475

55-
impl Trait for NonLeakS {}
56-
impl Trait for LeakS {}
57-
58-
fn main() {
59-
let _: &dyn Trait = &NonLeakS;
60-
//~^ ERROR the trait bound `NonLeakS: Leak` is not satisfied
61-
let _: &dyn Trait = &LeakS;
62-
let _: &(dyn Trait + ?Leak) = &LeakS;
63-
let x: &(dyn Trait + ?Leak) = &NonLeakS;
64-
x.leak_foo();
65-
//~^ ERROR the trait bound `dyn Trait: Leak` is not satisfied
66-
x.maybe_leak_foo();
76+
fn dyn_compat_check() {
77+
trait DynCompatCheck1: ?Leak {
78+
fn foo(&self) {}
79+
}
80+
81+
trait DynCompatCheck2: ?Leak {
82+
fn mut_foo(&mut self) {}
83+
}
84+
85+
impl DynCompatCheck1 for NonLeakS {}
86+
impl DynCompatCheck2 for NonLeakS {}
87+
88+
let _: &(dyn DynCompatCheck1 + ?Leak) = &NonLeakS;
89+
// There is no `?Leak` bound on corresponding `DispatchFromDyn` impl.
90+
let _: &dyn DynCompatCheck2 = &NonLeakS;
91+
//~^ ERROR the trait `DynCompatCheck2` is not dyn compatible
92+
}
93+
94+
fn args_check() {
95+
trait LeakTr {}
96+
6797
// Ensure that we validate the generic args of relaxed bounds in trait object types.
68-
let _: dyn Trait + ?Leak<(), Undefined = ()>;
98+
let _: dyn LeakTr + ?Leak<(), Undefined = ()>;
6999
//~^ ERROR trait takes 0 generic arguments but 1 generic argument was supplied
70100
//~| ERROR associated type `Undefined` not found for `Leak`
71101
}
102+
103+
fn main() {}

0 commit comments

Comments
 (0)