Skip to content

Commit

Permalink
Auto merge of rust-lang#121676 - Bryanskiy:polarity, r=petrochenkov
Browse files Browse the repository at this point in the history
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#120706 (comment)).

This is a part of the [MCP: Low level components for async drop](rust-lang/compiler-team#727)
  • Loading branch information
bors committed Jul 26, 2024
2 parents 95ee06c + dc49aa3 commit 2acbd31
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
if !lt.is_elided() {
self.unelided_trait_object_lifetime = true;
}
for bound in bounds {
for (bound, _) in bounds {
self.visit_poly_trait_ref(bound);
}
},
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {

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

for bound in bounds.iter().skip(1) {
for (bound, _) in bounds.iter().skip(1) {
bounds_span = bounds_span.to(bound.span);
}

Expand Down
19 changes: 11 additions & 8 deletions clippy_lints/src/types/borrowed_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m

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

false
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/types/type_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
TyKind::TraitObject(param_bounds, _, _) => {
let has_lifetime_parameters = param_bounds.iter().any(|bound| {
bound
.0
.bound_generic_params
.iter()
.any(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
Expand Down

0 comments on commit 2acbd31

Please sign in to comment.