Skip to content

Commit ee4ec2d

Browse files
authored
Unrolled build for #145879
Rollup merge of #145879 - Bryanskiy:supertraits-2, r=lcnr default auto traits: use default supertraits instead of `Self: Trait` bounds on associated items First commit: the motivation has been discussed [here](#144679). Second commit: the only new places where new implicit `DefaultAutoTrait` bounds are generated are supertraits and trait object so `?Trait` syntax should be extended to these places only. r? `@lcnr`
2 parents 565a9ca + 3ab7b39 commit ee4ec2d

File tree

11 files changed

+209
-345
lines changed

11 files changed

+209
-345
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,24 +2101,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21012101
{
21022102
return;
21032103
}
2104-
if self.tcx.features().more_maybe_bounds() {
2105-
return;
2106-
}
21072104
}
21082105
RelaxedBoundPolicy::Forbidden(reason) => {
2109-
if self.tcx.features().more_maybe_bounds() {
2110-
return;
2111-
}
2112-
21132106
match reason {
21142107
RelaxedBoundForbiddenReason::TraitObjectTy => {
2108+
if self.tcx.features().more_maybe_bounds() {
2109+
return;
2110+
}
2111+
21152112
self.dcx().span_err(
21162113
span,
21172114
"relaxed bounds are not permitted in trait object types",
21182115
);
21192116
return;
21202117
}
21212118
RelaxedBoundForbiddenReason::SuperTrait => {
2119+
if self.tcx.features().more_maybe_bounds() {
2120+
return;
2121+
}
2122+
21222123
let mut diag = self.dcx().struct_span_err(
21232124
span,
21242125
"relaxed bounds are not permitted in supertrait bounds",

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: 15 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+
/// Adds `experimental_default_bounds` bounds to the supertrait bounds.
349235
pub(crate) fn add_default_super_traits(
350236
&self,
351237
trait_def_id: LocalDefId,
@@ -354,21 +240,21 @@ 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+
// Supertraits for auto trait are unsound according to the unstable book:
246+
// https://doc.rust-lang.org/beta/unstable-book/language-features/auto-traits.html#supertraits
247+
if self.tcx().trait_is_auto(trait_def_id.to_def_id()) {
358248
return;
359249
}
360250

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-
}
251+
self.add_default_traits(
252+
bounds,
253+
self.tcx().types.self_param,
254+
hir_bounds,
255+
Some((trait_def_id, hir_generics.predicates)),
256+
span,
257+
);
372258
}
373259

374260
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/trait-bounds/more_maybe_bounds.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
2121

2222
// FIXME: `?Trait1` should be rejected, `Trait1` isn't marked `#[lang = "default_traitN"]`.
2323
fn baz<T>() where T: Iterator<Item: ?Trait1> {}
24+
//~^ ERROR this relaxed bound is not permitted here
25+
26+
struct S1<T>(T);
27+
28+
impl<T> S1<T> {
29+
fn f() where T: ?Trait1 {}
30+
//~^ ERROR this relaxed bound is not permitted here
31+
}
32+
33+
trait Trait5<'a> {}
34+
35+
struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
36+
//~^ ERROR this relaxed bound is not permitted here
37+
//~| ERROR bound modifier `?` can only be applied to default traits like `Sized`
2438

2539
struct S;
2640
impl !Trait2 for S {}

tests/ui/trait-bounds/more_maybe_bounds.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
error: this relaxed bound is not permitted here
2+
--> $DIR/more_maybe_bounds.rs:23:37
3+
|
4+
LL | fn baz<T>() where T: Iterator<Item: ?Trait1> {}
5+
| ^^^^^^^
6+
|
7+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
8+
9+
error: this relaxed bound is not permitted here
10+
--> $DIR/more_maybe_bounds.rs:29:21
11+
|
12+
LL | fn f() where T: ?Trait1 {}
13+
| ^^^^^^^
14+
|
15+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
16+
17+
error: this relaxed bound is not permitted here
18+
--> $DIR/more_maybe_bounds.rs:35:34
19+
|
20+
LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
21+
| ^^^^^^^^^^^
22+
|
23+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
24+
125
error: bound modifier `?` can only be applied to default traits like `Sized`
226
--> $DIR/more_maybe_bounds.rs:17:20
327
|
@@ -16,5 +40,11 @@ error: bound modifier `?` can only be applied to default traits like `Sized`
1640
LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
1741
| ^^^^^^^
1842

19-
error: aborting due to 3 previous errors
43+
error: bound modifier `?` can only be applied to default traits like `Sized`
44+
--> $DIR/more_maybe_bounds.rs:35:34
45+
|
46+
LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
47+
| ^^^^^^^^^^^
48+
49+
error: aborting due to 7 previous errors
2050

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

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

0 commit comments

Comments
 (0)