Skip to content

Commit

Permalink
Forbid ?Trait bounds repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryanskiy committed Jul 26, 2024
1 parent 2a73553 commit fd9d0bf
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
26 changes: 16 additions & 10 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
}

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;
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
20 changes: 19 additions & 1 deletion tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
9 changes: 9 additions & 0 deletions tests/ui/traits/maybe-polarity-repeated.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 21 additions & 0 deletions tests/ui/traits/maybe-polarity-repeated.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit fd9d0bf

Please sign in to comment.