Rollup merge of #138954 - compiler-errors:hash-opaques, r=oli-obk

Ensure `define_opaque` attrs are accounted for in HIR hash

Fixes #138948

r? oli-obk
This commit is contained in:
Stuart Cook 2025-03-26 19:40:30 +11:00 committed by GitHub
commit 19a53b7d3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 28 additions and 3 deletions

View file

@ -623,7 +623,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Don't hash unless necessary, because it's expensive.
let (opt_hash_including_bodies, attrs_hash) =
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
self.tcx.hash_owner_nodes(node, &bodies, &attrs, define_opaque);
let num_nodes = self.item_local_id_counter.as_usize();
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };

View file

@ -14,7 +14,7 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
use rustc_hir::*;
use rustc_macros::{Decodable, Encodable, HashStable};
use rustc_span::{ErrorGuaranteed, ExpnId};
use rustc_span::{ErrorGuaranteed, ExpnId, Span};
use crate::query::Providers;
use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
@ -157,6 +157,7 @@ impl<'tcx> TyCtxt<'tcx> {
node: OwnerNode<'_>,
bodies: &SortedMap<ItemLocalId, &Body<'_>>,
attrs: &SortedMap<ItemLocalId, &[Attribute]>,
define_opaque: Option<&[(Span, LocalDefId)]>,
) -> (Option<Fingerprint>, Option<Fingerprint>) {
if self.needs_crate_hash() {
self.with_stable_hashing_context(|mut hcx| {
@ -168,6 +169,10 @@ impl<'tcx> TyCtxt<'tcx> {
let mut stable_hasher = StableHasher::new();
attrs.hash_stable(&mut hcx, &mut stable_hasher);
// Hash the defined opaque types, which are not present in the attrs.
define_opaque.hash_stable(&mut hcx, &mut stable_hasher);
let h2 = stable_hasher.finish();
(Some(h1), Some(h2))
})

View file

@ -1288,7 +1288,8 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
let bodies = Default::default();
let attrs = hir::AttributeMap::EMPTY;
let (opt_hash_including_bodies, _) = self.tcx.hash_owner_nodes(node, &bodies, &attrs.map);
let (opt_hash_including_bodies, _) =
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, attrs.define_opaque);
let node = node.into();
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
opt_hash_including_bodies,

View file

@ -0,0 +1,19 @@
//@ revisions: rpass1 cfail2
#![feature(type_alias_impl_trait)]
pub type Foo = impl Sized;
#[cfg_attr(rpass1, define_opaque())]
#[cfg_attr(cfail2, define_opaque(Foo))]
fn a() {
//[cfail2]~^ ERROR item does not constrain `Foo::{opaque#0}`
let _: Foo = b();
}
#[define_opaque(Foo)]
fn b() -> Foo {
()
}
fn main() {}