rust/tests/ui/trait-bounds/fix-dyn-sized-fn-param-sugg.rs
León Orell Valerian Liehr cdc3d701cb
Don't reject *multiple* relaxed bounds, reject *duplicate* ones.
Having multiple relaxed bounds like `?Sized + ?Iterator` is actually *fine*.
We actually want to reject *duplicate* relaxed bounds like `?Sized + ?Sized`
because these most certainly represent a user error.

Note that this doesn't mean that we accept more code because a bound like
`?Iterator` is still invalid as it's not relaxing a *default* trait and
the only way to define / use more default bounds is under the experimental
and internal feature `more_maybe_bounds` plus `lang_items` plus unstable
flag `-Zexperimental-default-bounds` (historical context: for the longest
time, bounds like `?Iterator` were actually allowed and lead to a hard
warning).

Ultimately, this simply *reframes* the diagnostic. The scope of
`more_maybe_bounds` / `-Zexperimental-default-bounds` remains unchanged
as well.
2025-07-18 12:24:56 +02:00

39 lines
1.6 KiB
Rust

// Test that we emit a correct structured suggestions for dynamically sized ("maybe unsized")
// function parameters.
// We used to emit a butchered suggestion if duplicate relaxed `Sized` bounds were present.
// issue: <https://github.com/rust-lang/rust/issues/127441>.
use std::fmt::Debug;
fn foo1<T: ?Sized>(a: T) {}
//~^ ERROR the size for values of type `T` cannot be known at compilation time
fn foo2<T: ?Sized + ?Sized>(a: T) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `T` cannot be known at compilation time
fn foo3<T: ?Sized + ?Sized + Debug>(a: T) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `T` cannot be known at compilation time
fn foo4<T: ?Sized + Debug + ?Sized >(a: T) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `T` cannot be known at compilation time
fn foo5(_: impl ?Sized) {}
//~^ ERROR the size for values of type `impl ?Sized` cannot be known at compilation time
fn foo6(_: impl ?Sized + ?Sized) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `impl ?Sized + ?Sized` cannot be known at compilation tim
fn foo7(_: impl ?Sized + ?Sized + Debug) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `impl ?Sized + ?Sized + Debug` cannot be known at compilation time
fn foo8(_: impl ?Sized + Debug + ?Sized ) {}
//~^ ERROR duplicate relaxed `Sized` bounds
//~| ERROR the size for values of type `impl ?Sized + Debug + ?Sized` cannot be known at compilation time
fn main() {}