diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index e2e67dbb574..e136f334729 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -1242,11 +1242,12 @@ fn analysis_interfaces(tcx: TyCtxt<'_>, (): ()) { if tcx.sess.opts.unstable_opts.public_api_hash && tcx.dep_graph.is_fully_enabled() && tcx.crate_types() == [CrateType::Rlib] - && !tcx.codegen_output_mode(()).0 + && (!tcx.codegen_output_mode(()).0 || tcx.sess.opts.unstable_opts.embed_metadata) { // These queries allocate definition indices for captured lifetimes and anonymous // associated types. Visit their owners in HIR order before interface checking can - // allocate the same definitions in a different order during an incremental check. + // allocate the same definitions in a different order during incremental compilation. + // Native rlibs with embedded metadata can reuse the same item prefix. for opaque in tcx.hir_crate_items(()).opaques() { let owner = tcx.local_def_id_to_hir_id(opaque).owner; let _ = tcx.resolve_bound_vars(owner); diff --git a/tests/run-make/incremental-opaque-definition-order/rmake.rs b/tests/run-make/incremental-opaque-definition-order/rmake.rs index d09a160b7c8..bbc95d4b06d 100644 --- a/tests/run-make/incremental-opaque-definition-order/rmake.rs +++ b/tests/run-make/incremental-opaque-definition-order/rmake.rs @@ -4,18 +4,27 @@ use run_make_support::{rfs, rustc}; fn main() { // A private body edit keeps all the opaque definitions and their source positions unchanged. - for value in [1, 2] { - rfs::write("private.rs", format!("#[expect(dead_code)] fn value() -> u32 {{ {value} }}")); - let output = rustc() - .input("lib.rs") - .edition("2024") - .crate_type("rlib") - .emit("metadata") - .incremental("incremental") - .arg("-Zpublic-api-hash") - .arg("-Zthreads=8") - .env("RUSTC_LOG", "ohm_metadata_reuse=info") - .run(); - assert_eq!(output.stderr_utf8().contains("reused metadata item prefix"), value == 2); + for (emit, cache) in [("metadata", "check"), ("metadata,link", "build")] { + for value in [1, 2] { + rfs::write( + "private.rs", + format!("#[expect(dead_code)] fn value() -> u32 {{ {value} }}"), + ); + let output = rustc() + .input("lib.rs") + .edition("2024") + .crate_type("rlib") + .emit(emit) + .incremental(cache) + .arg("-Zpublic-api-hash") + .arg("-Zthreads=8") + .env("RUSTC_LOG", "ohm_metadata_reuse=info") + .run(); + assert_eq!( + output.stderr_utf8().contains("reused metadata item prefix"), + value == 2, + "{emit}" + ); + } } }