-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Remove 'static requirement on try_as_dyn #150161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // ignore-tidy-filelength | ||
| use std::borrow::Cow; | ||
| use std::fmt; | ||
| use std::ops::ControlFlow; | ||
|
|
||
| use rustc_abi::ExternAbi; | ||
| use rustc_ast::attr::AttributeExt; | ||
|
|
@@ -16,6 +17,7 @@ pub use rustc_ast::{ | |
| MetaItemInner, MetaItemLit, Movability, Mutability, Pinnedness, UnOp, | ||
| }; | ||
| use rustc_data_structures::fingerprint::Fingerprint; | ||
| use rustc_data_structures::fx::FxHashSet; | ||
| use rustc_data_structures::sorted_map::SortedMap; | ||
| use rustc_data_structures::tagged_ptr::TaggedRef; | ||
| use rustc_error_messages::{DiagArgValue, IntoDiagArg}; | ||
|
|
@@ -35,7 +37,7 @@ use crate::attrs::AttributeKind; | |
| use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res}; | ||
| use crate::def_id::{DefId, LocalDefIdMap}; | ||
| pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId}; | ||
| use crate::intravisit::{FnKind, VisitorExt}; | ||
| use crate::intravisit::{FnKind, Visitor, VisitorExt}; | ||
| use crate::lints::DelayedLints; | ||
|
|
||
| #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)] | ||
|
|
@@ -4445,6 +4447,70 @@ pub struct Impl<'hir> { | |
| pub constness: Constness, | ||
| } | ||
|
|
||
| impl Impl<'_> { | ||
| pub fn is_fully_generic_for_reflection(&self) -> bool { | ||
| #[derive(Default)] | ||
| struct LifetimeFinder { | ||
| seen: FxHashSet<LocalDefId>, | ||
| } | ||
| impl<'v> Visitor<'v> for LifetimeFinder { | ||
| type Result = ControlFlow<()>; | ||
| fn visit_lifetime(&mut self, lifetime: &'v Lifetime) -> Self::Result { | ||
| match lifetime.kind { | ||
| LifetimeKind::Param(def_id) => { | ||
| if self.seen.insert(def_id) { | ||
| ControlFlow::Continue(()) | ||
| } else { | ||
| ControlFlow::Break(()) | ||
| } | ||
| } | ||
| LifetimeKind::ImplicitObjectLifetimeDefault | ||
| | LifetimeKind::Error | ||
| | LifetimeKind::Infer | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | LifetimeKind::Static => ControlFlow::Break(()), | ||
| } | ||
| } | ||
| } | ||
| let mut ty_lifetimes = LifetimeFinder::default(); | ||
| if ty_lifetimes.visit_ty_unambig(self.self_ty).is_break() { | ||
| return false; | ||
|
Comment on lines
+4475
to
+4476
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function also needs to test for unicity of type params, lest
|
||
| } | ||
| let ty_lifetimes = ty_lifetimes.seen; | ||
|
|
||
| #[derive(Default)] | ||
| struct LifetimeChecker { | ||
| ty_lifetimes: FxHashSet<LocalDefId>, | ||
| } | ||
| impl<'v> Visitor<'v> for LifetimeChecker { | ||
| type Result = ControlFlow<()>; | ||
| fn visit_lifetime(&mut self, lifetime: &'v Lifetime) -> Self::Result { | ||
| match lifetime.kind { | ||
| LifetimeKind::Param(def_id) => { | ||
| if self.ty_lifetimes.contains(&def_id) { | ||
| ControlFlow::Break(()) | ||
| } else { | ||
| ControlFlow::Continue(()) | ||
| } | ||
| } | ||
| LifetimeKind::ImplicitObjectLifetimeDefault | ||
| | LifetimeKind::Error | ||
| | LifetimeKind::Infer | ||
| | LifetimeKind::Static => ControlFlow::Continue(()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut checker = LifetimeChecker { ty_lifetimes }; | ||
|
|
||
| // Pessimistic: if any of the lifetimes used in the type show up in where bounds | ||
| // don't allow this impl to be used. | ||
| self.generics | ||
| .predicates | ||
| .iter() | ||
| .all(|pred| checker.visit_where_predicate(pred).is_continue()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, HashStable_Generic)] | ||
| pub struct TraitImplHeader<'hir> { | ||
| pub safety: Safety, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,13 +78,22 @@ where | |
| TypingMode::Coherence => Certainty::AMBIGUOUS, | ||
| TypingMode::Analysis { .. } | ||
| | TypingMode::Borrowck { .. } | ||
| | TypingMode::Reflection | ||
| | TypingMode::PostBorrowckAnalysis { .. } | ||
| | TypingMode::PostAnalysis => return Err(NoSolution), | ||
| }, | ||
|
|
||
| // Impl matches polarity | ||
| (ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive) | ||
| | (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => Certainty::Yes, | ||
| | (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => { | ||
| if let TypingMode::Reflection = ecx.typing_mode() | ||
| && !cx.is_fully_generic_for_reflection(impl_def_id) | ||
| { | ||
| return Err(NoSolution); | ||
|
Comment on lines
+89
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is way over my head, lol, but just to sanity-check: could this branch be hoisted to the beginning of the function, as in, any |
||
| } else { | ||
| Certainty::Yes | ||
| } | ||
| } | ||
|
|
||
| // Impl doesn't match polarity | ||
| (ty::ImplPolarity::Positive, ty::PredicatePolarity::Negative) | ||
|
|
@@ -1330,6 +1339,7 @@ where | |
| TypingMode::Coherence => return, | ||
| TypingMode::Analysis { .. } | ||
| | TypingMode::Borrowck { .. } | ||
| | TypingMode::Reflection | ||
| | TypingMode::PostBorrowckAnalysis { .. } | ||
| | TypingMode::PostAnalysis => {} | ||
| } | ||
|
|
@@ -1504,6 +1514,7 @@ where | |
| } | ||
| TypingMode::Coherence | ||
| | TypingMode::PostAnalysis | ||
| | TypingMode::Reflection | ||
| | TypingMode::Borrowck { defining_opaque_types: _ } | ||
| | TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ } => {} | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice in this function to stop tracking
for<'any>-kind of lifetimes: those are part of the monomorphised info of the type, are they not? But this might require setting up a separate set of known-for<>-quantified lifetimes that ought to be ignored rather thanseen-checked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, I decided to just ignore those for now (sound but pessimistic again), but should def document that