Skip to content
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

Problem running a method on the output of a function that returns an associated type ("missing optimized MIR") #117997

Closed
amjoshuamichael opened this issue Nov 16, 2023 · 3 comments · Fixed by #118715
Assignees
Labels
C-bug Category: This is a bug. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@amjoshuamichael
Copy link

This is a pretty common use case, so this bug has probably already been registered, but I couldn't find anything while searching around, so I'm reporting this here just in case. I ran this code:

lib.rs

pub use impl_mod::TraitImplementer as Implementer;

pub use trait_mod::get_assoc;

mod impl_mod {
    use crate::trait_mod::TraitWithAssocType;

    pub struct TraitImplementer { }
    pub struct AssociatedType { }

    impl AssociatedType {
        pub fn method_on_assoc(&self) -> i32 { todo!() }
    }

    impl TraitWithAssocType for TraitImplementer {
        type AssocType = AssociatedType;
    }
}

mod trait_mod {
    use crate::Implementer;

    pub fn get_assoc() -> <Implementer as TraitWithAssocType>::AssocType {
        todo!()
    }

    pub trait TraitWithAssocType {
        type AssocType;
    }
}

examples/failing_code.rs

pub fn main() {
    mycrate::get_assoc().method_on_assoc();
}

When running this code, I expect to simply get a not yet implemented error on the get_assoc() function.

Instead, I get this:

warning: method `method_on_assoc` is never used
  --> src/lib.rs:12:16
   |
11 |     impl AssociatedType {
   |     ------------------- method in this implementation
12 |         pub fn method_on_assoc(&self) -> i32 { todo!() }
   |                ^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `mycrate` (lib) generated 1 warning
error: missing optimized MIR for an item in the crate `mycrate`
   |
note: missing optimized MIR for this item (was the crate `mycrate` compiled with `--emit=metadata`?)
  --> /Users/aaroncruz/Desktop/serf/code/src/lib.rs:12:9
   |
12 |         pub fn method_on_assoc(&self) -> i32 { todo!() }
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `mycrate` (example "failing_code") due to previous error

Meta

rustc --version --verbose:

rustc 1.76.0-nightly (6b771f6b5 2023-11-15)
binary: rustc
commit-hash: 6b771f6b5a6c8b03b6322a9c77ac77cb346148f0
commit-date: 2023-11-15
host: aarch64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.5

get_assoc returns an AssociatedType, and method_on_assoc is not a trait method–it's just a regular impl method. Therefore, AFAIK, this should compile. Notably, making either trait_mod or impl_mod as pub makes it compile fine.

@amjoshuamichael amjoshuamichael added the C-bug Category: This is a bug. label Nov 16, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 16, 2023
@tmiasko tmiasko added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 17, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 17, 2023
@tmiasko
Copy link
Contributor

tmiasko commented Nov 17, 2023

Regression in c2ef351.
cc @oli-obk

@lqd
Copy link
Member

lqd commented Nov 17, 2023

oli's on leave so cc-ing PR #117076's reviewer @petrochenkov

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 20, 2023
@davidtwco davidtwco self-assigned this Dec 7, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2023
…it-trait-ref-and-args, r=TaKO8Ki

privacy: visit trait def id of projections

Fixes rust-lang#117997.

A refactoring in rust-lang#117076 changed the `DefIdVisitorSkeleton` to avoid calling `visit_projection_ty` for `ty::Projection` aliases, and instead just iterate over the args - this makes sense, as `visit_projection_ty` will indirectly visit all of the same args, but in doing so, will also create a `TraitRef` containing the trait's `DefId`, which also gets visited. The trait's `DefId` isn't visited when we only visit the arguments without separating them into `TraitRef` and own args first.

Eventually this influences the reachability set and whether a function is encoded into the metadata.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2023
…it-trait-ref-and-args, r=TaKO8Ki

privacy: visit trait def id of projections

Fixes rust-lang#117997.

A refactoring in rust-lang#117076 changed the `DefIdVisitorSkeleton` to avoid calling `visit_projection_ty` for `ty::Projection` aliases, and instead just iterate over the args - this makes sense, as `visit_projection_ty` will indirectly visit all of the same args, but in doing so, will also create a `TraitRef` containing the trait's `DefId`, which also gets visited. The trait's `DefId` isn't visited when we only visit the arguments without separating them into `TraitRef` and own args first.

Eventually this influences the reachability set and whether a function is encoded into the metadata.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 9, 2023
Rollup merge of rust-lang#118715 - davidtwco:issue-117997-privacy-visit-trait-ref-and-args, r=TaKO8Ki

privacy: visit trait def id of projections

Fixes rust-lang#117997.

A refactoring in rust-lang#117076 changed the `DefIdVisitorSkeleton` to avoid calling `visit_projection_ty` for `ty::Projection` aliases, and instead just iterate over the args - this makes sense, as `visit_projection_ty` will indirectly visit all of the same args, but in doing so, will also create a `TraitRef` containing the trait's `DefId`, which also gets visited. The trait's `DefId` isn't visited when we only visit the arguments without separating them into `TraitRef` and own args first.

Eventually this influences the reachability set and whether a function is encoded into the metadata.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants