Skip to content

Commit 1be9a86

Browse files
committed
Error on define_opaques entries without any opaques actually referenced
1 parent 2759552 commit 1be9a86

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
@@ -1694,7 +1694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16941694
);
16951695
return None;
16961696
};
1697-
Some(did)
1697+
Some((self.lower_span(path.span), did))
16981698
});
16991699
let define_opaques = self.arena.alloc_from_iter(define_opaques);
17001700
self.define_opaques.insert(hir_id.local_id, 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: SortedMap<hir::ItemLocalId, &'hir [LocalDefId]>,
103+
define_opaques: SortedMap<hir::ItemLocalId, &'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: SortedMap<ItemLocalId, &'tcx [LocalDefId]>,
1311+
pub define_opaques: SortedMap<ItemLocalId, &'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
@@ -181,11 +181,14 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
181181
else {
182182
return;
183183
};
184-
for &define in defines {
184+
for &(span, define) in defines {
185185
trace!(?define);
186186
let mode = std::mem::replace(&mut self.mode, CollectionMode::Taits);
187-
// TODO: check that opaque types were introduced and error otherwise (also add tests)
187+
let n = self.opaques.len();
188188
super::sig_types::walk_types(self.tcx, define, self);
189+
if n == self.opaques.len() {
190+
self.tcx.dcx().span_err(span, "item does not contain any opaque types");
191+
}
189192
self.mode = mode;
190193
}
191194
// 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)