-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Trait upcasting coercion (part4) #88010
Conversation
r? @davidtwco (rust-highfive has picked a reviewer for you, use r? to override) |
The job Click to see the possible cause of the failure (guessed by this bot)
|
@@ -625,3 +628,27 @@ fn promoted_mir<'tcx>( | |||
|
|||
tcx.arena.alloc(promoted) | |||
} | |||
|
|||
pub(crate) fn custom_coerce_unsize_info<'tcx>( |
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.
Maybe this function would be better put somewhere in rustc_mir/src/util
?
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.
should i create a new module in util
for this?
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.
If none of the others seem to fit, yeah.
@@ -132,6 +132,24 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> { | |||
self.register_violations(&violations, &unsafe_blocks); | |||
} | |||
}, | |||
Rvalue::Cast(kind, source, mir_cast_ty) => { | |||
if let CastKind::Pointer(ty::adjustment::PointerCast::Unsize) = kind { | |||
let mir_source_ty = match *source { |
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.
You should be able to use Operand::ty
here, right?
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.
I'm not totally sure, since this is earlier than codegen phase, if i'm not misunderstanding it. I'll take a look.
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.
This has nothing to do with codegen. I think you basically inlined Operand::ty
here.
fn is_trait_upcasting_coercion_pointee(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> bool { | ||
match (source.kind(), target.kind()) { | ||
(&ty::Dynamic(data_a, ..), &ty::Dynamic(data_b, ..)) => { | ||
data_a.principal_def_id() != data_b.principal_def_id() |
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.
Why is it okay to allow casts where data_a.principal_def_id() == data_b.principal_def_id()
? Please add a comment.
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.
Ok!
//~^ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
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.
Why are these two separate files?
Please also add some casts that are okay.
Does this hit the last match arm in is_trait_upcasting_coercion_from_raw_pointer
?
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.
This is a separate file because the "mismatched types" error will prevent unsafety checker from running at all, it seems. I don't really know whether error recovery is possible at all in this case. This test case is just the status quo. I'll leave comments, when the unsafety checking implementation is ready.
Please also add some casts that are okay.
I will, thanks!
Does this hit the last match arm in is_trait_upcasting_coercion_from_raw_pointer?
No it doesn't, the unsafety checker is not yet running in this case.
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.
No it doesn't, the unsafety checker is not yet running in this case.
Okay, then the other test should definitely do something to hit that arm.
Good question. Currently you seem to be doing some kind of recursion here, but I am not sure what this means in terms of the resulting behavior. Is it correct that that Cc @rust-lang/lang |
@RalfJung i think the answer is yes, according to On the other hand the reference says something unintuitive: It says I think my current problem is that, the |
Yeah, it sounds like for full expressivity we'd need For now, I think assuming they are safe to cast (if the recursive check succeeds) makes sense. But this should be added to the list of unresolved questions for trait coercions, to be brought up in the stabilization report. Also there should be tests for things like |
I'll let this PR wait on #88135 a little, and discuss more about |
I'll be pretty busy for some time since I am moving, so I am probably not the best reviewer here. |
That's ok, after some discussions i think this is not the prefered approach here. Closing for now, but might get back to this if necessary. |
This is a incomplete version of trait upcasting unsafety checking. I think it's working in principle, however i think i meet a problem here. How can i know
Box<T>
orArc<T>
's internal pointer can be seen as "always-valid"?cc @RalfJung