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](https://github.com/rust-lang/rust/pull/120706#issuecomment-1934006762). This is a part of the [MCP: Low level components for async drop](https://github.com/rust-lang/compiler-team/issues/727)
This commit is contained in:
commit
2acbd31686
@ -543,7 +543,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
|
|||||||
if !lt.is_elided() {
|
if !lt.is_elided() {
|
||||||
self.unelided_trait_object_lifetime = true;
|
self.unelided_trait_object_lifetime = true;
|
||||||
}
|
}
|
||||||
for bound in bounds {
|
for (bound, _) in bounds {
|
||||||
self.visit_poly_trait_ref(bound);
|
self.visit_poly_trait_ref(bound);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
|||||||
|
|
||||||
// Iterate the bounds and add them to our seen hash
|
// Iterate the bounds and add them to our seen hash
|
||||||
// If we haven't yet seen it, add it to the fixed traits
|
// 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 {
|
let Some(def_id) = bound.trait_ref.trait_def_id() else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@ -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,
|
// 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
|
// there must be 1 or more duplicates
|
||||||
if bounds.len() != unique_traits.len() {
|
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);
|
bounds_span = bounds_span.to(bound.span);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.
|
// Returns true if given type is `Any` trait.
|
||||||
fn is_any_trait(cx: &LateContext<'_>, t: &hir::Ty<'_>) -> bool {
|
fn is_any_trait(cx: &LateContext<'_>, t: &hir::Ty<'_>) -> bool {
|
||||||
if let TyKind::TraitObject(traits, ..) = t.kind
|
if let TyKind::TraitObject(traits, ..) = t.kind {
|
||||||
&& !traits.is_empty()
|
return traits
|
||||||
&& let Some(trait_did) = traits[0].trait_ref.trait_def_id()
|
.iter()
|
||||||
// Only Send/Sync can be used as additional traits, so it is enough to
|
.any(|(bound, _)| {
|
||||||
// check only the first trait.
|
if let Some(trait_did) = bound.trait_ref.trait_def_id()
|
||||||
&& cx.tcx.is_diagnostic_item(sym::Any, trait_did)
|
&& cx.tcx.is_diagnostic_item(sym::Any, trait_did)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
false
|
||||||
|
@ -55,6 +55,7 @@ impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
|
|||||||
TyKind::TraitObject(param_bounds, _, _) => {
|
TyKind::TraitObject(param_bounds, _, _) => {
|
||||||
let has_lifetime_parameters = param_bounds.iter().any(|bound| {
|
let has_lifetime_parameters = param_bounds.iter().any(|bound| {
|
||||||
bound
|
bound
|
||||||
|
.0
|
||||||
.bound_generic_params
|
.bound_generic_params
|
||||||
.iter()
|
.iter()
|
||||||
.any(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
|
.any(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user