Error on define_opaques entries without any opaques actually referenced

This commit is contained in:
Oli Scherer 2025-02-25 10:31:11 +00:00
parent 43e39260f9
commit 69a1bb8bdb
6 changed files with 17 additions and 7 deletions

View File

@ -1696,7 +1696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
return None;
};
Some(did)
Some((self.lower_span(path.span), did))
});
let define_opaque = self.arena.alloc_from_iter(define_opaque);
self.define_opaque = Some(define_opaque);

View File

@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir> {
/// Bodies inside the owner being lowered.
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
/// `#[define_opaque]` attributes
define_opaque: Option<&'hir [LocalDefId]>,
define_opaque: Option<&'hir [(Span, LocalDefId)]>,
/// Attributes inside the owner being lowered.
attrs: SortedMap<hir::ItemLocalId, &'hir [hir::Attribute]>,
/// Collect items that were created by lowering the current owner.

View File

@ -1308,7 +1308,7 @@ impl Attribute {
pub struct AttributeMap<'tcx> {
pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
/// Preprocessed `#[define_opaque]` attribute.
pub define_opaque: Option<&'tcx [LocalDefId]>,
pub define_opaque: Option<&'tcx [(Span, LocalDefId)]>,
// Only present when the crate hash is needed.
pub opt_hash: Option<Fingerprint>,
}

View File

@ -190,11 +190,14 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
let Some(defines) = self.tcx.hir_attrs(hir_id.owner).define_opaque else {
return;
};
for &define in defines {
for &(span, define) in defines {
trace!(?define);
let mode = std::mem::replace(&mut self.mode, CollectionMode::Taits);
// TODO: check that opaque types were introduced and error otherwise (also add tests)
let n = self.opaques.len();
super::sig_types::walk_types(self.tcx, define, self);
if n == self.opaques.len() {
self.tcx.dcx().span_err(span, "item does not contain any opaque types");
}
self.mode = mode;
}
// Allow using `#[define_opaque]` on assoc methods and type aliases to override the default collection mode in

View File

@ -1,8 +1,7 @@
//@ check-pass
#![feature(type_alias_impl_trait)]
type Thing = ();
#[define_opaque(Thing)]
//~^ ERROR item does not contain any opaque types
fn main() {}

View File

@ -0,0 +1,8 @@
error: item does not contain any opaque types
--> $DIR/no_opaque.rs:5:17
|
LL | #[define_opaque(Thing)]
| ^^^^^
error: aborting due to 1 previous error