From e14043c2e4bdeb88c1a2a755f76bc1d7a585de4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jana=20D=C3=B6nszelmann?= Date: Fri, 6 Nov 2026 14:05:15 +0100 Subject: [PATCH] rewrite relaxed bound policy code --- compiler/rustc_ast_lowering/src/item.rs | 27 ++- compiler/rustc_ast_lowering/src/lib.rs | 203 ++++++++++-------- ...issing-associated_item_or_field_def_ids.rs | 1 - ...ng-associated_item_or_field_def_ids.stderr | 8 +- ...ing-associated-items-of-undefined-trait.rs | 1 - ...associated-items-of-undefined-trait.stderr | 8 +- .../feature-gate-more-maybe-bounds.rs | 6 +- .../feature-gate-more-maybe-bounds.stderr | 20 +- tests/ui/parser/trait-object-trait-parens.rs | 6 +- .../parser/trait-object-trait-parens.stderr | 6 +- .../ui/sized-hierarchy/default-supertrait.rs | 8 +- .../sized-hierarchy/default-supertrait.stderr | 38 ++-- tests/ui/traits/alias/relaxed-bounds.rs | 2 +- tests/ui/traits/alias/relaxed-bounds.stderr | 2 +- .../unsized/relaxed-bounds-invalid-places.rs | 13 +- .../relaxed-bounds-invalid-places.stderr | 38 ++-- 16 files changed, 192 insertions(+), 195 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index a7f138c992517..902215eda27aa 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -458,7 +458,10 @@ impl<'hir> LoweringContext<'_, 'hir> { |this| { let bounds = this.lower_param_bounds( bounds, - RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait), + RelaxedBoundPolicy::forbidden( + &mut Default::default(), + RelaxedBoundForbiddenReason::SuperTrait, + ), ImplTraitContext::Disallowed(ImplTraitPosition::Bound), ); let items = this.arena.alloc_from_iter( @@ -488,7 +491,10 @@ impl<'hir> LoweringContext<'_, 'hir> { |this| { this.lower_param_bounds( bounds, - RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::TraitAlias), + RelaxedBoundPolicy::forbidden( + &mut Default::default(), + RelaxedBoundForbiddenReason::TraitAlias, + ), ImplTraitContext::Disallowed(ImplTraitPosition::Bound), ) }, @@ -932,7 +938,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::TraitItemKind::Type( this.lower_param_bounds( bounds, - RelaxedBoundPolicy::Allowed(&mut Default::default()), + RelaxedBoundPolicy::allow_all(&mut Default::default()), ImplTraitContext::Disallowed(ImplTraitPosition::Generic), ), ty, @@ -1793,7 +1799,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ¶m.bounds, param.colon_span, generics.span, - RelaxedBoundPolicy::Allowed( + RelaxedBoundPolicy::allow_all( dedup_map.entry(self.local_def_id(param.id)).or_default(), ), itctx, @@ -1957,15 +1963,22 @@ impl<'hir> LoweringContext<'_, 'hir> { bounded_ty, bounds, }) => { + let mut forbidden_dedup_map; let rbp = if bound_generic_params.is_empty() && let Some(res) = self.get_partial_res(bounded_ty.id).and_then(|r| r.full_res()) && let Res::Def(DefKind::TyParam, def_id) = res && params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id()) { - RelaxedBoundPolicy::Allowed(dedup_map.entry(def_id.expect_local()).or_default()) + RelaxedBoundPolicy::allow_all( + dedup_map.entry(def_id.expect_local()).or_default(), + ) } else { - RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::WhereBound) + forbidden_dedup_map = Default::default(); + RelaxedBoundPolicy::forbidden( + &mut forbidden_dedup_map, + RelaxedBoundForbiddenReason::WhereBound, + ) }; hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate { bound_generic_params: self.lower_generic_params( @@ -1993,7 +2006,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ), bounds: self.lower_param_bounds( bounds, - RelaxedBoundPolicy::Allowed(&mut Default::default()), + RelaxedBoundPolicy::allow_all(&mut Default::default()), ImplTraitContext::Disallowed(ImplTraitPosition::Bound), ), in_where_clause: true, diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 6b4240b666532..0ee12d988db2f 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -446,22 +446,53 @@ impl<'tcx> ResolverAstLowering<'tcx> { } } +bitflags::bitflags! { + #[derive(Copy, Clone, Debug)] + struct AllowedRelaxedBounds: u32 { + const SIZED = 1 << 0; + const FORGET = 1 << 1; + // any other relaxed bound, see `#[feature(more_maybe_bounds)]` + const OTHER = 1 << 2; + } +} + /// How relaxed bounds `?Trait` should be treated. /// /// Relaxed bounds should only be allowed in places where we later /// (namely during HIR ty lowering) perform *sized elaboration*. #[derive(Debug)] -enum RelaxedBoundPolicy<'a> { +struct RelaxedBoundPolicy<'a> { /// The `DefId` refers to the trait that is being relaxed. - Allowed(&'a mut FxIndexMap), - Forbidden(RelaxedBoundForbiddenReason), + dedup: &'a mut FxIndexMap, + reason: Option, + allowed_bounds: AllowedRelaxedBounds, } -impl RelaxedBoundPolicy<'_> { + +impl<'a> RelaxedBoundPolicy<'a> { + fn allow_all(dedup: &'a mut FxIndexMap) -> Self { + Self { dedup, reason: None, allowed_bounds: AllowedRelaxedBounds::all() } + } + + fn forbidden( + dedup: &'a mut FxIndexMap, + reason: RelaxedBoundForbiddenReason, + ) -> Self { + Self { dedup, reason: Some(reason), allowed_bounds: AllowedRelaxedBounds::empty() } + } + + // FIXME(forget_trait) this will become used when Forget is added. + #[allow(unused)] + fn allow_some( + dedup: &'a mut FxIndexMap, + reason: RelaxedBoundForbiddenReason, + allowed_bounds: AllowedRelaxedBounds, + ) -> Self { + Self { dedup, reason: Some(reason), allowed_bounds } + } + fn reborrow(&mut self) -> RelaxedBoundPolicy<'_> { - match self { - RelaxedBoundPolicy::Allowed(m) => RelaxedBoundPolicy::Allowed(m), - RelaxedBoundPolicy::Forbidden(reason) => RelaxedBoundPolicy::Forbidden(*reason), - } + let &mut Self { ref mut dedup, reason, allowed_bounds } = self; + RelaxedBoundPolicy { dedup, reason, allowed_bounds } } } @@ -1339,7 +1370,10 @@ impl<'hir> LoweringContext<'_, 'hir> { } else { let bounds = self.lower_param_bounds( bounds, - RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::AssocTyBounds), + RelaxedBoundPolicy::forbidden( + &mut Default::default(), + RelaxedBoundForbiddenReason::AssocTyBounds, + ), itctx, ); hir::AssocItemConstraintKind::Bound { bounds } @@ -1496,7 +1530,10 @@ impl<'hir> LoweringContext<'_, 'hir> { span: t.span, parens: ast::Parens::No, }, - RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::TraitObjectTy), + RelaxedBoundPolicy::forbidden( + &mut Default::default(), + RelaxedBoundForbiddenReason::TraitObjectTy, + ), itctx, ); let bounds = this.arena.alloc_from_iter([bound]); @@ -1623,7 +1660,8 @@ impl<'hir> LoweringContext<'_, 'hir> { GenericBound::Trait(ty) => { let trait_ref = this.lower_poly_trait_ref( ty, - RelaxedBoundPolicy::Forbidden( + RelaxedBoundPolicy::forbidden( + &mut Default::default(), RelaxedBoundForbiddenReason::TraitObjectTy, ), itctx, @@ -1683,7 +1721,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ImplTraitContext::InBinding => { hir::TyKind::TraitAscription(self.lower_param_bounds( bounds, - RelaxedBoundPolicy::Allowed(&mut Default::default()), + RelaxedBoundPolicy::allow_all(&mut Default::default()), itctx, )) } @@ -1845,7 +1883,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_opaque_inner(opaque_ty_node_id, origin, opaque_ty_span, |this| { this.lower_param_bounds( bounds, - RelaxedBoundPolicy::Allowed(&mut Default::default()), + RelaxedBoundPolicy::allow_all(&mut Default::default()), itctx, ) }) @@ -2485,84 +2523,81 @@ impl<'hir> LoweringContext<'_, 'hir> { span: Span, rbp: RelaxedBoundPolicy<'_>, ) { - // Even though feature `more_maybe_bounds` enables the user to relax all default bounds - // other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't - // want to advertise it to the user (via a feature gate error) since it's super internal. - // - // FIXME(more_maybe_bounds): Moreover, if we actually were to add proper default traits - // (like a hypothetical `Move` or `Leak`) we would want to validate the location according - // to default trait elaboration in HIR ty lowering (which depends on the specific trait in - // question: E.g., `?Sized` & `?Move` most likely won't be allowed in all the same places). - - match rbp { - RelaxedBoundPolicy::Allowed(dedup_map) => { - // `trait_def_id` only returns `None` for errors during resolution. - let Some(trait_def_id) = trait_ref.trait_def_id() else { return }; - let tcx = self.tcx; - let err = |s| { - let name = tcx.item_name(trait_def_id); - tcx.dcx() - .struct_span_err( - vec![span, s], - format!("duplicate relaxed `{name}` bounds"), + let tcx = self.tcx; + + let deny = |trait_name: Option<&str>| { + let (context, subject) = match rbp.reason { + Some(RelaxedBoundForbiddenReason::TraitObjectTy) => { + ("trait object types", "trait object types") + } + Some(RelaxedBoundForbiddenReason::SuperTrait) => ("supertrait bounds", "traits"), + Some(RelaxedBoundForbiddenReason::TraitAlias) => { + ("trait alias bounds", "trait aliases") + } + Some(RelaxedBoundForbiddenReason::AssocTyBounds) + | Some(RelaxedBoundForbiddenReason::WhereBound) + | None => { + self.dcx() + .struct_span_err(span, "this relaxed bound is not permitted here") + .with_note( + "in this context, relaxed bounds are only allowed on \ + type parameters defined on the closest item", ) - .with_code(E0203) .emit(); - }; - dedup_map.entry(trait_def_id).and_modify(|&mut s| err(s)).or_insert(span); - return; + return; + } + }; + + let mut diag = self.dcx().struct_span_err( + span, + format!( + "relaxed {}bounds are not permitted in {context}", + trait_name.map(|i| format!("`{i}` ")).unwrap_or_default() + ), + ); + if let Some(name) = trait_name { + diag.note(format!( + "{subject} are not implicitly bounded by `{name}`, \ + so there is nothing to relax" + )); } - RelaxedBoundPolicy::Forbidden(reason) => { - let gate = |context, subject| { - let extended = self.tcx.features().more_maybe_bounds(); - let is_sized = trait_ref - .trait_def_id() - .is_some_and(|def_id| self.tcx.is_lang_item(def_id, LangItem::Sized)); - - if extended && !is_sized { - return; - } + diag.emit(); + }; - let prefix = if extended { "`Sized` " } else { "" }; - let mut diag = self.dcx().struct_span_err( - span, - format!("relaxed {prefix}bounds are not permitted in {context}"), - ); - if is_sized { - diag.note(format!( - "{subject} are not implicitly bounded by `Sized`, \ - so there is nothing to relax" - )); - } - diag.emit(); - }; + // `trait_def_id` only returns `None` for errors during resolution, + // and there's no need to re-report those. + let Some(trait_def_id) = trait_ref.trait_def_id() else { return }; - match reason { - RelaxedBoundForbiddenReason::TraitObjectTy => { - gate("trait object types", "trait object types"); - return; - } - RelaxedBoundForbiddenReason::SuperTrait => { - gate("supertrait bounds", "traits"); - return; - } - RelaxedBoundForbiddenReason::TraitAlias => { - gate("trait alias bounds", "trait aliases"); - return; - } - RelaxedBoundForbiddenReason::AssocTyBounds - | RelaxedBoundForbiddenReason::WhereBound => {} - }; + if self.tcx.is_lang_item(trait_def_id, LangItem::Sized) { + if !rbp.allowed_bounds.contains(AllowedRelaxedBounds::SIZED) { + return deny(Some("Sized")); + } + } else if self.tcx.features().more_maybe_bounds() + && !rbp.allowed_bounds.contains(AllowedRelaxedBounds::OTHER) + { + // Even though feature `more_maybe_bounds` enables the user to relax all default bounds + // other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't + // want to advertise it to the user (via a feature gate error) since it's super internal. + match rbp.reason { + Some(RelaxedBoundForbiddenReason::TraitObjectTy) + | Some(RelaxedBoundForbiddenReason::SuperTrait) + | Some(RelaxedBoundForbiddenReason::TraitAlias) + | None => { + return; + } + Some(RelaxedBoundForbiddenReason::AssocTyBounds) + | Some(RelaxedBoundForbiddenReason::WhereBound) => return deny(None), } } - self.dcx() - .struct_span_err(span, "this relaxed bound is not permitted here") - .with_note( - "in this context, relaxed bounds are only allowed on \ - type parameters defined on the closest item", - ) - .emit(); + let err = |s| { + let name = tcx.item_name(trait_def_id); + tcx.dcx() + .struct_span_err(vec![span, s], format!("duplicate relaxed `{name}` bounds")) + .with_code(E0203) + .emit(); + }; + rbp.dedup.entry(trait_def_id).and_modify(|&mut s| err(s)).or_insert(span); } #[instrument(level = "debug", skip(self), ret)] @@ -2615,7 +2650,7 @@ impl<'hir> LoweringContext<'_, 'hir> { bounds, /* colon_span */ None, span, - RelaxedBoundPolicy::Allowed(&mut Default::default()), + RelaxedBoundPolicy::allow_all(&mut Default::default()), ImplTraitContext::Universal, hir::PredicateOrigin::ImplTrait, ); diff --git a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs index f4b05749db97f..32edc67c57820 100644 --- a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs +++ b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs @@ -2,6 +2,5 @@ fn main() -> dyn Iterator + ?Iterator::advance_by(usize) { //~^ ERROR cannot find trait `advance_by` in trait `Iterator` - //~| ERROR relaxed bounds are not permitted in trait object types todo!() } diff --git a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr index ab9db8cfb5438..8f7a7d3f97717 100644 --- a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr +++ b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr @@ -6,12 +6,6 @@ LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) { | = note: an associated function named `Iterator::advance_by` exists in another namespace -error: relaxed bounds are not permitted in trait object types - --> $DIR/missing-associated_item_or_field_def_ids.rs:3:29 - | -LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0404`. diff --git a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs index bac4e60886772..3f0d42a52061c 100644 --- a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs +++ b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs @@ -7,5 +7,4 @@ trait Tr { fn main() { let _: dyn Tr + ?Foo; //~^ ERROR: cannot find trait `Foo` in this scope - //~| ERROR: relaxed bounds are not permitted in trait object types } diff --git a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr index 3660fcece62ae..a440d27a23be5 100644 --- a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr +++ b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr @@ -4,12 +4,6 @@ error[E0405]: cannot find trait `Foo` in this scope LL | let _: dyn Tr + ?Foo; | ^^^ not found in this scope -error: relaxed bounds are not permitted in trait object types - --> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:21 - | -LL | let _: dyn Tr + ?Foo; - | ^^^^^^^^^^^^^^^^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0405`. diff --git a/tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs b/tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs index 191fef3eae457..61ea51d61e1d7 100644 --- a/tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs +++ b/tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs @@ -4,14 +4,12 @@ #[lang = "default_trait2"] auto trait Trait2 {} trait Trait3: ?Trait1 {} -//~^ ERROR relaxed bounds are not permitted in supertrait bounds -//~| ERROR bound modifier `?` can only be applied to `Sized` +//~^ ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` fn foo(_: Box) {} -//~^ ERROR relaxed bounds are not permitted in trait object types -//~| ERROR bound modifier `?` can only be applied to `Sized` +//~^ ERROR bound modifier `?` can only be applied to `Sized` fn bar(_: T) {} //~^ ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` diff --git a/tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr b/tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr index 6503af2b5876d..4c8660f293623 100644 --- a/tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr +++ b/tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr @@ -1,15 +1,3 @@ -error: relaxed bounds are not permitted in supertrait bounds - --> $DIR/feature-gate-more-maybe-bounds.rs:6:15 - | -LL | trait Trait3: ?Trait1 {} - | ^^^^^^^ - -error: relaxed bounds are not permitted in trait object types - --> $DIR/feature-gate-more-maybe-bounds.rs:12:28 - | -LL | fn foo(_: Box) {} - | ^^^^^^^ - error: bound modifier `?` can only be applied to `Sized` --> $DIR/feature-gate-more-maybe-bounds.rs:6:15 | @@ -33,22 +21,22 @@ LL | trait Trait3: ?Trait1 {} = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error: bound modifier `?` can only be applied to `Sized` - --> $DIR/feature-gate-more-maybe-bounds.rs:12:28 + --> $DIR/feature-gate-more-maybe-bounds.rs:11:28 | LL | fn foo(_: Box) {} | ^^^^^^^ error: bound modifier `?` can only be applied to `Sized` - --> $DIR/feature-gate-more-maybe-bounds.rs:15:11 + --> $DIR/feature-gate-more-maybe-bounds.rs:13:11 | LL | fn bar(_: T) {} | ^^^^^^^ error: bound modifier `?` can only be applied to `Sized` - --> $DIR/feature-gate-more-maybe-bounds.rs:15:21 + --> $DIR/feature-gate-more-maybe-bounds.rs:13:21 | LL | fn bar(_: T) {} | ^^^^^^^ -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors diff --git a/tests/ui/parser/trait-object-trait-parens.rs b/tests/ui/parser/trait-object-trait-parens.rs index 2b124c8c94bfd..a769417ba2f10 100644 --- a/tests/ui/parser/trait-object-trait-parens.rs +++ b/tests/ui/parser/trait-object-trait-parens.rs @@ -7,17 +7,17 @@ fn f Trait<'a>)>() {} fn main() { let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>; - //~^ ERROR relaxed bounds are not permitted in trait object types + //~^ ERROR relaxed `Sized` bounds are not permitted in trait object types //~| ERROR only auto traits can be used as additional traits //~| WARN trait objects without an explicit `dyn` are deprecated //~| WARN this is accepted in the current edition let _: Box Trait<'a>) + (Obj)>; - //~^ ERROR relaxed bounds are not permitted in trait object types + //~^ ERROR relaxed `Sized` bounds are not permitted in trait object types //~| ERROR only auto traits can be used as additional traits //~| WARN trait objects without an explicit `dyn` are deprecated //~| WARN this is accepted in the current edition let _: Box Trait<'a> + (Obj) + (?Sized)>; - //~^ ERROR relaxed bounds are not permitted in trait object types + //~^ ERROR relaxed `Sized` bounds are not permitted in trait object types //~| ERROR only auto traits can be used as additional traits //~| WARN trait objects without an explicit `dyn` are deprecated //~| WARN this is accepted in the current edition diff --git a/tests/ui/parser/trait-object-trait-parens.stderr b/tests/ui/parser/trait-object-trait-parens.stderr index 9f6a47a5e9819..493494b4a3414 100644 --- a/tests/ui/parser/trait-object-trait-parens.stderr +++ b/tests/ui/parser/trait-object-trait-parens.stderr @@ -1,4 +1,4 @@ -error: relaxed bounds are not permitted in trait object types +error: relaxed `Sized` bounds are not permitted in trait object types --> $DIR/trait-object-trait-parens.rs:9:24 | LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>; @@ -6,7 +6,7 @@ LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>; | = note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types +error: relaxed `Sized` bounds are not permitted in trait object types --> $DIR/trait-object-trait-parens.rs:14:16 | LL | let _: Box Trait<'a>) + (Obj)>; @@ -14,7 +14,7 @@ LL | let _: Box Trait<'a>) + (Obj)>; | = note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types +error: relaxed `Sized` bounds are not permitted in trait object types --> $DIR/trait-object-trait-parens.rs:19:44 | LL | let _: Box Trait<'a> + (Obj) + (?Sized)>; diff --git a/tests/ui/sized-hierarchy/default-supertrait.rs b/tests/ui/sized-hierarchy/default-supertrait.rs index b13f9d90081cd..9987427caa052 100644 --- a/tests/ui/sized-hierarchy/default-supertrait.rs +++ b/tests/ui/sized-hierarchy/default-supertrait.rs @@ -6,21 +6,19 @@ use std::marker::{MetaSized, PointeeSized}; trait Sized_: Sized { } trait NegSized: ?Sized { } -//~^ ERROR relaxed bounds are not permitted in supertrait bounds +//~^ ERROR relaxed `Sized` bounds are not permitted in supertrait bounds trait MetaSized_: MetaSized { } trait NegMetaSized: ?MetaSized { } -//~^ ERROR relaxed bounds are not permitted in supertrait bounds -//~| ERROR bound modifier `?` can only be applied to `Sized` +//~^ ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` trait PointeeSized_: PointeeSized { } trait NegPointeeSized: ?PointeeSized { } -//~^ ERROR relaxed bounds are not permitted in supertrait bounds -//~| ERROR bound modifier `?` can only be applied to `Sized` +//~^ ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` //~| ERROR bound modifier `?` can only be applied to `Sized` diff --git a/tests/ui/sized-hierarchy/default-supertrait.stderr b/tests/ui/sized-hierarchy/default-supertrait.stderr index 35875163774d4..e5d541edf79ed 100644 --- a/tests/ui/sized-hierarchy/default-supertrait.stderr +++ b/tests/ui/sized-hierarchy/default-supertrait.stderr @@ -1,4 +1,4 @@ -error: relaxed bounds are not permitted in supertrait bounds +error: relaxed `Sized` bounds are not permitted in supertrait bounds --> $DIR/default-supertrait.rs:8:17 | LL | trait NegSized: ?Sized { } @@ -6,18 +6,6 @@ LL | trait NegSized: ?Sized { } | = note: traits are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in supertrait bounds - --> $DIR/default-supertrait.rs:13:21 - | -LL | trait NegMetaSized: ?MetaSized { } - | ^^^^^^^^^^ - -error: relaxed bounds are not permitted in supertrait bounds - --> $DIR/default-supertrait.rs:21:24 - | -LL | trait NegPointeeSized: ?PointeeSized { } - | ^^^^^^^^^^^^^ - error: bound modifier `?` can only be applied to `Sized` --> $DIR/default-supertrait.rs:13:21 | @@ -41,13 +29,13 @@ LL | trait NegMetaSized: ?MetaSized { } = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error: bound modifier `?` can only be applied to `Sized` - --> $DIR/default-supertrait.rs:21:24 + --> $DIR/default-supertrait.rs:20:24 | LL | trait NegPointeeSized: ?PointeeSized { } | ^^^^^^^^^^^^^ error: bound modifier `?` can only be applied to `Sized` - --> $DIR/default-supertrait.rs:21:24 + --> $DIR/default-supertrait.rs:20:24 | LL | trait NegPointeeSized: ?PointeeSized { } | ^^^^^^^^^^^^^ @@ -55,7 +43,7 @@ LL | trait NegPointeeSized: ?PointeeSized { } = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error: bound modifier `?` can only be applied to `Sized` - --> $DIR/default-supertrait.rs:21:24 + --> $DIR/default-supertrait.rs:20:24 | LL | trait NegPointeeSized: ?PointeeSized { } | ^^^^^^^^^^^^^ @@ -63,7 +51,7 @@ LL | trait NegPointeeSized: ?PointeeSized { } = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error[E0277]: the size for values of type `T` cannot be known at compilation time - --> $DIR/default-supertrait.rs:40:22 + --> $DIR/default-supertrait.rs:38:22 | LL | fn with_metasized_supertrait() { | - this type parameter needs to be `Sized` @@ -71,13 +59,13 @@ LL | requires_sized::(); | ^ doesn't have a size known at compile-time | note: required by a bound in `requires_sized` - --> $DIR/default-supertrait.rs:29:22 + --> $DIR/default-supertrait.rs:27:22 | LL | fn requires_sized() {} | ^^^^^ required by this bound in `requires_sized` error[E0277]: the size for values of type `T` cannot be known at compilation time - --> $DIR/default-supertrait.rs:48:22 + --> $DIR/default-supertrait.rs:46:22 | LL | fn with_pointeesized_supertrait() { | - this type parameter needs to be `Sized` @@ -85,19 +73,19 @@ LL | requires_sized::(); | ^ doesn't have a size known at compile-time | note: required by a bound in `requires_sized` - --> $DIR/default-supertrait.rs:29:22 + --> $DIR/default-supertrait.rs:27:22 | LL | fn requires_sized() {} | ^^^^^ required by this bound in `requires_sized` error[E0277]: the size for values of type `T` cannot be known - --> $DIR/default-supertrait.rs:50:26 + --> $DIR/default-supertrait.rs:48:26 | LL | requires_metasized::(); | ^ doesn't have a known size | note: required by a bound in `requires_metasized` - --> $DIR/default-supertrait.rs:30:26 + --> $DIR/default-supertrait.rs:28:26 | LL | fn requires_metasized() {} | ^^^^^^^^^ required by this bound in `requires_metasized` @@ -107,7 +95,7 @@ LL | fn with_pointeesized_supertrait $DIR/default-supertrait.rs:57:22 + --> $DIR/default-supertrait.rs:55:22 | LL | fn with_bare_trait() { | - this type parameter needs to be `Sized` @@ -115,11 +103,11 @@ LL | requires_sized::(); | ^ doesn't have a size known at compile-time | note: required by a bound in `requires_sized` - --> $DIR/default-supertrait.rs:29:22 + --> $DIR/default-supertrait.rs:27:22 | LL | fn requires_sized() {} | ^^^^^ required by this bound in `requires_sized` -error: aborting due to 13 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/alias/relaxed-bounds.rs b/tests/ui/traits/alias/relaxed-bounds.rs index 88e092c631622..97e733adc9077 100644 --- a/tests/ui/traits/alias/relaxed-bounds.rs +++ b/tests/ui/traits/alias/relaxed-bounds.rs @@ -3,7 +3,7 @@ // Ensure that relaxed bounds are not permitted in the `Self` bounds of trait aliases because trait // aliases (like traits) aren't implicitly bounded by `Sized` so there's nothing to relax. -trait Alias0 = ?Sized; //~ ERROR relaxed bounds are not permitted in trait alias bounds +trait Alias0 = ?Sized; //~ ERROR relaxed `Sized` bounds are not permitted in trait alias bounds trait Alias1 = where Self: ?Sized; //~ ERROR this relaxed bound is not permitted here trait Alias2 =; // OK diff --git a/tests/ui/traits/alias/relaxed-bounds.stderr b/tests/ui/traits/alias/relaxed-bounds.stderr index d7782b723a305..a1a2b8e5428ff 100644 --- a/tests/ui/traits/alias/relaxed-bounds.stderr +++ b/tests/ui/traits/alias/relaxed-bounds.stderr @@ -1,4 +1,4 @@ -error: relaxed bounds are not permitted in trait alias bounds +error: relaxed `Sized` bounds are not permitted in trait alias bounds --> $DIR/relaxed-bounds.rs:6:16 | LL | trait Alias0 = ?Sized; diff --git a/tests/ui/unsized/relaxed-bounds-invalid-places.rs b/tests/ui/unsized/relaxed-bounds-invalid-places.rs index 5aa1dbb9a094e..1b06dc3890c56 100644 --- a/tests/ui/unsized/relaxed-bounds-invalid-places.rs +++ b/tests/ui/unsized/relaxed-bounds-invalid-places.rs @@ -12,8 +12,7 @@ struct S3(T) where &'static T: ?Sized; //~ ERROR this relaxed bound is not pe trait Trait<'a> {} struct S4(T) where for<'a> T: ?Trait<'a>; -//~^ ERROR this relaxed bound is not permitted here -//~| ERROR bound modifier `?` can only be applied to `Sized` +//~^ ERROR bound modifier `?` can only be applied to `Sized` struct S5(*const T) where T: ?Trait<'static> + ?Sized; //~^ ERROR bound modifier `?` can only be applied to `Sized` @@ -26,16 +25,16 @@ impl S1 { // issue: struct S6(T) where T: Iterator; //~ ERROR this relaxed bound is not permitted here -trait Tr: ?Sized {} //~ ERROR relaxed bounds are not permitted in supertrait bounds +trait Tr: ?Sized {} //~ ERROR relaxed `Sized` bounds are not permitted in supertrait bounds // Test that relaxed `Sized` bounds are rejected in trait object types: type O0 = dyn ?Sized; -//~^ ERROR relaxed bounds are not permitted in trait object types +//~^ ERROR relaxed `Sized` bounds are not permitted in trait object types //~| ERROR at least one trait is required for an object type [E0224] -type O1 = dyn Tr + ?Sized; //~ ERROR relaxed bounds are not permitted in trait object types +type O1 = dyn Tr + ?Sized; //~ ERROR relaxed `Sized` bounds are not permitted in trait object types type O2 = dyn ?Sized + ?Sized + Tr; -//~^ ERROR relaxed bounds are not permitted in trait object types -//~| ERROR relaxed bounds are not permitted in trait object types +//~^ ERROR relaxed `Sized` bounds are not permitted in trait object types +//~| ERROR relaxed `Sized` bounds are not permitted in trait object types fn main() {} diff --git a/tests/ui/unsized/relaxed-bounds-invalid-places.stderr b/tests/ui/unsized/relaxed-bounds-invalid-places.stderr index 7f54d045a1ce8..ea834a13907e5 100644 --- a/tests/ui/unsized/relaxed-bounds-invalid-places.stderr +++ b/tests/ui/unsized/relaxed-bounds-invalid-places.stderr @@ -23,15 +23,7 @@ LL | struct S3(T) where &'static T: ?Sized; = note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item error: this relaxed bound is not permitted here - --> $DIR/relaxed-bounds-invalid-places.rs:14:34 - | -LL | struct S4(T) where for<'a> T: ?Trait<'a>; - | ^^^^^^^^^^ - | - = note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item - -error: this relaxed bound is not permitted here - --> $DIR/relaxed-bounds-invalid-places.rs:22:21 + --> $DIR/relaxed-bounds-invalid-places.rs:21:21 | LL | fn f() where T: ?Sized {} | ^^^^^^ @@ -39,47 +31,47 @@ LL | fn f() where T: ?Sized {} = note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item error: this relaxed bound is not permitted here - --> $DIR/relaxed-bounds-invalid-places.rs:27:41 + --> $DIR/relaxed-bounds-invalid-places.rs:26:41 | LL | struct S6(T) where T: Iterator; | ^^^^^^ | = note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item -error: relaxed bounds are not permitted in supertrait bounds - --> $DIR/relaxed-bounds-invalid-places.rs:29:11 +error: relaxed `Sized` bounds are not permitted in supertrait bounds + --> $DIR/relaxed-bounds-invalid-places.rs:28:11 | LL | trait Tr: ?Sized {} | ^^^^^^ | = note: traits are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types - --> $DIR/relaxed-bounds-invalid-places.rs:33:15 +error: relaxed `Sized` bounds are not permitted in trait object types + --> $DIR/relaxed-bounds-invalid-places.rs:32:15 | LL | type O0 = dyn ?Sized; | ^^^^^^ | = note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types - --> $DIR/relaxed-bounds-invalid-places.rs:36:20 +error: relaxed `Sized` bounds are not permitted in trait object types + --> $DIR/relaxed-bounds-invalid-places.rs:35:20 | LL | type O1 = dyn Tr + ?Sized; | ^^^^^^ | = note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types - --> $DIR/relaxed-bounds-invalid-places.rs:37:15 +error: relaxed `Sized` bounds are not permitted in trait object types + --> $DIR/relaxed-bounds-invalid-places.rs:36:15 | LL | type O2 = dyn ?Sized + ?Sized + Tr; | ^^^^^^ | = note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax -error: relaxed bounds are not permitted in trait object types - --> $DIR/relaxed-bounds-invalid-places.rs:37:24 +error: relaxed `Sized` bounds are not permitted in trait object types + --> $DIR/relaxed-bounds-invalid-places.rs:36:24 | LL | type O2 = dyn ?Sized + ?Sized + Tr; | ^^^^^^ @@ -93,17 +85,17 @@ LL | struct S4(T) where for<'a> T: ?Trait<'a>; | ^^^^^^^^^^ error: bound modifier `?` can only be applied to `Sized` - --> $DIR/relaxed-bounds-invalid-places.rs:18:33 + --> $DIR/relaxed-bounds-invalid-places.rs:17:33 | LL | struct S5(*const T) where T: ?Trait<'static> + ?Sized; | ^^^^^^^^^^^^^^^ error[E0224]: at least one trait is required for an object type - --> $DIR/relaxed-bounds-invalid-places.rs:33:11 + --> $DIR/relaxed-bounds-invalid-places.rs:32:11 | LL | type O0 = dyn ?Sized; | ^^^^^^^^^^ -error: aborting due to 14 previous errors +error: aborting due to 13 previous errors For more information about this error, try `rustc --explain E0224`.