Skip to content

Commit 2acbd31

Browse files
committed
Auto merge of #121676 - Bryanskiy:polarity, r=petrochenkov
Support ?Trait bounds in supertraits and dyn Trait under a feature gate This patch allows `maybe` polarity bounds under a feature gate. The only language change here is that corresponding hard errors are replaced by feature gates. Example: ```rust #![feature(allow_maybe_polarity)] ... trait Trait1 : ?Trait { ... } // ok fn foo(_: Box<(dyn Trait2 + ?Trait)>) {} // ok fn bar<T: ?Sized + ?Trait>(_: &T) {} // ok ``` Maybe bounds still don't do anything (except for `Sized` trait), however this patch will allow us to [experiment with default auto traits](rust-lang/rust#120706 (comment)). This is a part of the [MCP: Low level components for async drop](rust-lang/compiler-team#727)
2 parents 95ee06c + dc49aa3 commit 2acbd31

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

clippy_lints/src/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
543543
if !lt.is_elided() {
544544
self.unelided_trait_object_lifetime = true;
545545
}
546-
for bound in bounds {
546+
for (bound, _) in bounds {
547547
self.visit_poly_trait_ref(bound);
548548
}
549549
},

clippy_lints/src/trait_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
183183

184184
// Iterate the bounds and add them to our seen hash
185185
// If we haven't yet seen it, add it to the fixed traits
186-
for bound in bounds {
186+
for (bound, _) in bounds {
187187
let Some(def_id) = bound.trait_ref.trait_def_id() else {
188188
continue;
189189
};
@@ -198,9 +198,9 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
198198
// If the number of unique traits isn't the same as the number of traits in the bounds,
199199
// there must be 1 or more duplicates
200200
if bounds.len() != unique_traits.len() {
201-
let mut bounds_span = bounds[0].span;
201+
let mut bounds_span = bounds[0].0.span;
202202

203-
for bound in bounds.iter().skip(1) {
203+
for (bound, _) in bounds.iter().skip(1) {
204204
bounds_span = bounds_span.to(bound.span);
205205
}
206206

clippy_lints/src/types/borrowed_box.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
8181

8282
// Returns true if given type is `Any` trait.
8383
fn is_any_trait(cx: &LateContext<'_>, t: &hir::Ty<'_>) -> bool {
84-
if let TyKind::TraitObject(traits, ..) = t.kind
85-
&& !traits.is_empty()
86-
&& let Some(trait_did) = traits[0].trait_ref.trait_def_id()
87-
// Only Send/Sync can be used as additional traits, so it is enough to
88-
// check only the first trait.
89-
&& cx.tcx.is_diagnostic_item(sym::Any, trait_did)
90-
{
91-
return true;
84+
if let TyKind::TraitObject(traits, ..) = t.kind {
85+
return traits
86+
.iter()
87+
.any(|(bound, _)| {
88+
if let Some(trait_did) = bound.trait_ref.trait_def_id()
89+
&& cx.tcx.is_diagnostic_item(sym::Any, trait_did)
90+
{
91+
return true;
92+
}
93+
false
94+
});
9295
}
9396

9497
false

clippy_lints/src/types/type_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
5555
TyKind::TraitObject(param_bounds, _, _) => {
5656
let has_lifetime_parameters = param_bounds.iter().any(|bound| {
5757
bound
58+
.0
5859
.bound_generic_params
5960
.iter()
6061
.any(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))

0 commit comments

Comments
 (0)