Remove 'static requirement on try_as_dyn#150161
Conversation
|
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
This comment has been minimized.
This comment has been minimized.
e7ef1ee to
29f1dba
Compare
|
r? BoxyUwU |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
29f1dba to
fe33b0c
Compare
This comment has been minimized.
This comment has been minimized.
dfa5c33 to
30f5641
Compare
This comment has been minimized.
This comment has been minimized.
|
The hacky solution is obviously not a general fix. But I think it's progress. As a next step I will add the input type as a generic parameter on |
This comment was marked as resolved.
This comment was marked as resolved.
hmm... and desugar |
ae17524 to
ceb4679
Compare
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ceb4679 to
9aba3c3
Compare
This comment has been minimized.
This comment has been minimized.
9aba3c3 to
a85463d
Compare
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
a85463d to
0b2da7a
Compare
This comment has been minimized.
This comment has been minimized.
0b2da7a to
676173f
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
676173f to
67f7c3d
Compare
This comment has been minimized.
This comment has been minimized.
|
|
||
| fn impl_is_fully_generic_for_reflection(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { | ||
| tcx.impl_trait_header(def_id).is_fully_generic_for_reflection() | ||
| && tcx.explicit_predicates_of(def_id).is_fully_generic_for_reflection() |
There was a problem hiding this comment.
If we don't need to check elaborated clauses does that mean we actually don't need to check any predicates since we'll wind up proving them in Reflection mode too
There was a problem hiding this comment.
well, we do need to prevent outlives bounds, but I guess we could allow any trait bound that doesn't repeat generic params or uses 'static
| } | ||
|
|
||
| /// Allow simple where bounds like `T: Debug`, but prevent any kind of | ||
| /// outlives bounds or uses of generic parameters on the right hand side. |
There was a problem hiding this comment.
this feels kind of arbitrary to me, Self parameters are not special in any way in the type system afaik, what's the reason we want this
There was a problem hiding this comment.
The reason is basically that things like T: Debug get checked for fully-genericness when the trait solver actually gets to the impl for it, so recursively everything will be fine. There is probably more we can allow, but this one was easy to reason about
| PostBorrowckAnalysis { defined_opaque_types: I::LocalDefIds }, | ||
| /// During the evaluation of reflection logic that ignores lifetimes, we can only | ||
| /// handle impls that are fully generic over all lifetimes without constraints on | ||
| /// those lifetimes (other than implied bounds). |
There was a problem hiding this comment.
why other than implied bounds? unlike normal specialization we don't have a base impl whose lifetime constraints have been checked to hold which we can rely on.
impl<T> Trait for &'_ T seems problematic for try_as_dyn given noone can ever actually check T: '_
There was a problem hiding this comment.
i guess you can rely on wfness of the traitref the user wrote for the try_as_dyn 🤔 so yeah maybe that is fine?
| return Err(()); | ||
| match self.infcx.typing_mode() { | ||
| TypingMode::Coherence => {} | ||
| TypingMode::Reflection |
There was a problem hiding this comment.
why do we not handle reservation impls in reflection mode. is there a test for try_as_dyn'ing a trait ref which has a reservation impl
There was a problem hiding this comment.
We do handle them, this is just the code path for when we fail to resolve the impl anyway. In case the impl is fully generic for reflection we fall through to the normal code path below
|
@rustbot author |
| } | ||
|
|
||
| /// Returns `Some(&U)` if `T` can be coerced to the trait object type `U`. Otherwise, it returns `None`. | ||
| /// Trait that is automatically implemented for all `dyn Trait<'b, C> + 'a` without assoc type bounds. |
There was a problem hiding this comment.
why no assoc type bounds? this trait just exists to ensure that the type being try_as_dyn'd outlives the lifetime bound of the resulting trait object right?
There was a problem hiding this comment.
We'd need to find some other way to handle
#![feature(type_info, ptr_metadata, arbitrary_self_types_pointers)]
use std::any::TypeId;
use std::ptr::{self, DynMetadata};
type Payload = Box<i32>;
trait Trait {
type Assoc;
fn method(self: *const Self, value: Self::Assoc) -> &'static Payload;
}
struct Thing;
impl Trait for Thing {
type Assoc = &'static Payload;
fn method(self: *const Self, value: Self::Assoc) -> &'static Payload {
value
}
}
fn extend<'a>(payload: &'a Payload) -> &'static Payload {
let metadata: DynMetadata<dyn Trait<Assoc = &'a Payload>> = const {
TypeId::of::<Thing>()
.trait_info_of::<dyn Trait<Assoc = &'a Payload>>()
//~^ ERROR `dyn Trait<Assoc = &'a Box<i32>>: TryAsDynCompatible<'_>` is not satisfied
.unwrap()
.get_vtable()
};
let ptr: *const dyn Trait<Assoc = &'a Payload> =
ptr::from_raw_parts(std::ptr::null::<()>(), metadata);
ptr.method(payload)
}
fn main() {
let payload: Box<Payload> = Box::new(Box::new(1i32));
let wrong: &'static Payload = extend(&*payload);
drop(payload);
println!("{wrong}");
}So I punted on all assoc types
This comment has been minimized.
This comment has been minimized.
67f7c3d to
0d0eec0
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
View all comments
tracking issue: #144361
cc @ivarflakstad @izagawd