Skip to content

Commit f67a283

Browse files
committed
Error on define_opaques entries without any opaques actually referenced
1 parent 4d4255f commit f67a283

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16961696
);
16971697
return None;
16981698
};
1699-
Some(did)
1699+
Some((self.lower_span(path.span), did))
17001700
});
17011701
let define_opaques = self.arena.alloc_from_iter(define_opaques);
17021702
self.define_opaques = Some(define_opaques);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct LoweringContext<'a, 'hir> {
100100
/// Bodies inside the owner being lowered.
101101
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
102102
/// `#[define_opaques]` attributes
103-
define_opaques: Option<&'hir [LocalDefId]>,
103+
define_opaques: Option<&'hir [(Span, LocalDefId)]>,
104104
/// Attributes inside the owner being lowered.
105105
attrs: SortedMap<hir::ItemLocalId, &'hir [hir::Attribute]>,
106106
/// Collect items that were created by lowering the current owner.

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ impl Attribute {
13081308
pub struct AttributeMap<'tcx> {
13091309
pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
13101310
/// Preprocessed `#[define_opaques]` attribute.
1311-
pub define_opaques: Option<&'tcx [LocalDefId]>,
1311+
pub define_opaques: Option<&'tcx [(Span, LocalDefId)]>,
13121312
// Only present when the crate hash is needed.
13131313
pub opt_hash: Option<Fingerprint>,
13141314
}

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
183183
let Some(defines) = self.tcx.hir_attrs(hir_id.owner).define_opaques else {
184184
return;
185185
};
186-
for &define in defines {
186+
for &(span, define) in defines {
187187
trace!(?define);
188188
let mode = std::mem::replace(&mut self.mode, CollectionMode::Taits);
189-
// TODO: check that opaque types were introduced and error otherwise (also add tests)
189+
let n = self.opaques.len();
190190
super::sig_types::walk_types(self.tcx, define, self);
191+
if n == self.opaques.len() {
192+
self.tcx.dcx().span_err(span, "item does not contain any opaque types");
193+
}
191194
self.mode = mode;
192195
}
193196
// Allow using `#[define_opaques]` on assoc methods and type aliases to override the default collection mode in
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//@ check-pass
2-
31
#![feature(type_alias_impl_trait)]
42

53
type Thing = ();
64

75
#[define_opaques(Thing)]
6+
//~^ ERROR item does not contain any opaque types
87
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: item does not contain any opaque types
2+
--> $DIR/no_opaque.rs:5:18
3+
|
4+
LL | #[define_opaques(Thing)]
5+
| ^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)