-
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
Detect when 'static
obligation might come from an impl
#73783
Detect when 'static
obligation might come from an impl
#73783
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs
Outdated
Show resolved
Hide resolved
r? @Aaron1011 |
"cannot infer an appropriate lifetime" | ||
} else { | ||
err.span_label(sup_origin.span(), "...is captured here..."); | ||
if return_sp < sup_origin.span() { |
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 for DiagnosticBuilder
to have a concept of 'ordered diagnostics', as there are lots of other places where this kind of logic would be useful. However, it doesn't have to block this PR.
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 had a proposal around this at some point. I wanted to change the rendering if the diagnostics would not display in source order.
src/test/ui/associated-types/cache/project-fn-ret-invariant.transmute.stderr
Show resolved
Hide resolved
src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
05475b4
to
bdb39ab
Compare
.. | ||
}) => Some(*span), | ||
})) if of_trait.trait_def_id() == Some(trait_did) => Some(self_ty), |
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.
Just realized this is wrong, this is meant to check the self_ty instead. It happens to work in the test because it is the only impl.
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 using an alternative guessing method, but eventually we would want to do the check between self_ty
and the original method receiver ty, but I always find it tricky to correctly compare hir::Ty
s and ty::Ty
s.
|
||
// Get the `Ident` of the method being called and the corresponding `impl` (to point at | ||
// `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called). | ||
let (ident, self_ty) = match tcx.hir().get_if_local(instance.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.
We almost have enough information to do this for foreign crates - we can get the method ident (through AssocItem
), and the overall impl
span, but not the span of the self type. I think it's fine to just open an issue about extending this error message, though.
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.
For foreign crates we might want to just say "the impl
in another crate causes the 'static
requirement, deal with it or bother their author 🤷♂️" 😬
48f9664
to
5136013
Compare
@Aaron1011 are there any items left to tackle? |
(Changing the labels as I presume they were changed due to #73783 (comment). The new heuristic isn't perfect, but should work well enough.) |
let parent_id = tcx.hir().get_parent_item(*hir_id); | ||
match tcx.hir().find(parent_id) { | ||
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => { | ||
// The method being called is defined in the `trait`, but the `'static` |
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 I understand correctly, this is handling a case like:
trait OtherTrait {}
trait Foo {
fn bar(&self) {}
}
impl Foo for dyn OtherTrait {}
where we're using the default implementation from the trait?
I think we can remove the need for this by extending Instance::resolve
to provide information about the parent impl
item (even when the method implementation) comes from the trait itself
). Could you open an issue to track changing 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.
I just reused this method for the Trait::method(binding)
case, but I'll look into modifying Instance::resolve
regardless.
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 did a quick test modifying Instance::resolve and I caused a bunch of unrelated ICEs. I'll do that in a follow up PR.
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 fine with merging this as-is, unless there are other changes that you'd like to make.
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 satisfied with the cases I'm handling. I'll revisit this in the future for further improvement but I think can be merged. (Of courses take a last review pass to make sure I didn't introduce anything by accident.)
src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs
Show resolved
Hide resolved
src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs
Outdated
Show resolved
Hide resolved
@bors r=nikomatsakis |
📌 Commit 7b05fb5 has been approved by |
…time, r=nikomatsakis Detect when `'static` obligation might come from an `impl` Partly address rust-lang#71341.
7b05fb5
to
889a4d9
Compare
@bors r=nikomatsakis |
📌 Commit 889a4d9 has been approved by |
🌲 The tree is currently closed for pull requests below priority 5, this pull request will be tested once the tree is reopened |
…arth Rollup of 9 pull requests Successful merges: - rust-lang#73783 (Detect when `'static` obligation might come from an `impl`) - rust-lang#73868 (Advertise correct stable version for const control flow) - rust-lang#74460 (rustdoc: Always warn when linking from public to private items) - rust-lang#74538 (Guard against non-monomorphized type_id intrinsic call) - rust-lang#74541 (Add the aarch64-apple-darwin target ) - rust-lang#74600 (Enable perf try builder) - rust-lang#74618 (Do not ICE on assoc type with bad placeholder) - rust-lang#74631 (rustc_target: Add a target spec option for disabling `--eh-frame-hdr`) - rust-lang#74643 (build: Remove unnecessary `cargo:rerun-if-env-changed` annotations) Failed merges: r? @ghost
Partly address #71341.