Skip to content

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

Closed
@amjoshuamichael

Description

@amjoshuamichael

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.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions