Forbid ?Trait bounds repetitions

This commit is contained in:
Bryanskiy 2024-07-26 16:33:30 +03:00
parent 2a73553513
commit fd9d0bfbac
5 changed files with 72 additions and 11 deletions

View File

@ -75,16 +75,22 @@ pub(crate) fn add_sized_bound(
}
}
if unbounds.len() > 1 && !tcx.features().more_maybe_bounds {
self.tcx()
.sess
.create_feature_err(
errors::MultipleRelaxedDefaultBounds {
spans: unbounds.iter().map(|ptr| ptr.span).collect(),
},
sym::more_maybe_bounds,
)
.emit();
let mut unique_bounds = FxIndexSet::default();
let mut seen_repeat = false;
for unbound in &unbounds {
if let Res::Def(DefKind::Trait, unbound_def_id) = unbound.trait_ref.path.res {
seen_repeat |= !unique_bounds.insert(unbound_def_id);
}
}
if unbounds.len() > 1 {
let err = errors::MultipleRelaxedDefaultBounds {
spans: unbounds.iter().map(|ptr| ptr.span).collect(),
};
if seen_repeat {
self.dcx().emit_err(err);
} else if !tcx.features().more_maybe_bounds {
self.tcx().sess.create_feature_err(err, sym::more_maybe_bounds).emit();
};
}
let mut seen_sized_unbound = false;

View File

@ -14,4 +14,11 @@ fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
trait Trait {}
// Do not suggest `#![feature(more_maybe_bounds)]` for repetitions
fn baz<T: ?Trait + ?Trait>(_ : T) {}
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
fn main() {}

View File

@ -47,7 +47,25 @@ warning: relaxing a default bound only does something for `?Sized`; all other tr
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
| ^^^^^^^
error: aborting due to 4 previous errors; 2 warnings emitted
error[E0203]: type parameter has more than one relaxed default bound, only one is supported
--> $DIR/feature-gate-more-maybe-bounds.rs:19:11
|
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
| ^^^^^^ ^^^^^^
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/feature-gate-more-maybe-bounds.rs:19:11
|
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
| ^^^^^^
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/feature-gate-more-maybe-bounds.rs:19:20
|
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
| ^^^^^^
error: aborting due to 5 previous errors; 4 warnings emitted
Some errors have detailed explanations: E0203, E0658.
For more information about an error, try `rustc --explain E0203`.

View File

@ -0,0 +1,9 @@
#![feature(more_maybe_bounds)]
trait Trait {}
fn foo<T: ?Trait + ?Trait>(_: T) {}
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
//~| WARN relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
fn main() {}

View File

@ -0,0 +1,21 @@
error[E0203]: type parameter has more than one relaxed default bound, only one is supported
--> $DIR/maybe-polarity-repeated.rs:4:11
|
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}
| ^^^^^^ ^^^^^^
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/maybe-polarity-repeated.rs:4:11
|
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}
| ^^^^^^
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/maybe-polarity-repeated.rs:4:20
|
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}
| ^^^^^^
error: aborting due to 1 previous error; 2 warnings emitted
For more information about this error, try `rustc --explain E0203`.