Skip to content

Commit 354a7be

Browse files
committed
Error on define_opaques entries without any opaques actually referenced
1 parent f4f03be commit 354a7be

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_index::{IndexSlice, IndexVec};
1111
use rustc_middle::span_bug;
1212
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1313
use rustc_span::edit_distance::find_best_match_for_name;
14+
use rustc_span::source_map::{Spanned, respan};
1415
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
1516
use smallvec::{SmallVec, smallvec};
1617
use thin_vec::ThinVec;
@@ -1678,7 +1679,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16781679
pub(super) fn lower_define_opaques(
16791680
&mut self,
16801681
define_opaques: &Option<ThinVec<(NodeId, Path)>>,
1681-
) -> Option<&'hir [LocalDefId]> {
1682+
) -> Option<&'hir [Spanned<LocalDefId>]> {
16821683
define_opaques.as_ref().map(|d| {
16831684
&*self.arena.alloc_from_iter(d.iter().filter_map(|(id, path)| {
16841685
let res = self.resolver.get_partial_res(*id).unwrap();
@@ -1693,7 +1694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16931694
);
16941695
return None;
16951696
};
1696-
Some(did)
1697+
Some(respan(self.lower_span(path.span), did))
16971698
}))
16981699
})
16991700
}

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub struct Generics<'hir> {
704704
pub has_where_clause_predicates: bool,
705705
pub where_clause_span: Span,
706706
pub span: Span,
707-
pub define_opaques: Option<&'hir [LocalDefId]>,
707+
pub define_opaques: Option<&'hir [Spanned<LocalDefId>]>,
708708
}
709709

710710
impl<'hir> Generics<'hir> {
@@ -1436,7 +1436,7 @@ pub struct Closure<'hir> {
14361436
/// The span of the argument block `|...|`
14371437
pub fn_arg_span: Option<Span>,
14381438
pub kind: ClosureKind,
1439-
pub define_opaques: Option<&'hir [LocalDefId]>,
1439+
pub define_opaques: Option<&'hir [Spanned<LocalDefId>]>,
14401440
}
14411441

14421442
#[derive(Clone, PartialEq, Eq, Debug, Copy, Hash, HashStable_Generic, Encodable, Decodable)]
@@ -4087,7 +4087,7 @@ pub enum ItemKind<'hir> {
40874087
Use(&'hir UsePath<'hir>, UseKind),
40884088

40894089
/// A `static` item.
4090-
Static(&'hir Ty<'hir>, Mutability, BodyId, Option<&'hir [LocalDefId]>),
4090+
Static(&'hir Ty<'hir>, Mutability, BodyId, Option<&'hir [Spanned<LocalDefId>]>),
40914091
/// A `const` item.
40924092
Const(&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
40934093
/// A function declaration.
@@ -4619,7 +4619,7 @@ impl<'hir> Node<'hir> {
46194619
}
46204620
}
46214621

4622-
pub fn define_opaques(&self) -> Option<&'hir [LocalDefId]> {
4622+
pub fn define_opaques(&self) -> Option<&'hir [Spanned<LocalDefId>]> {
46234623
match self {
46244624
Node::Item(Item { kind: ItemKind::Static(.., define_opaques), .. }) => *define_opaques,
46254625
Node::Expr(Expr { kind: ExprKind::Closure(c), .. }) => c.define_opaques,

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
181181
return;
182182
};
183183
for &define in defines {
184-
// TODO: check that `define` is a type alias (and add tests)
185184
trace!(?define);
186185
let mode = std::mem::replace(&mut self.mode, CollectionMode::Taits);
187-
// TODO: check that opaque types were introduced and error otherwise (also add tests)
188-
super::sig_types::walk_types(self.tcx, define, self);
186+
let n = self.opaques.len();
187+
super::sig_types::walk_types(self.tcx, define.node, self);
188+
if n == self.opaques.len() {
189+
self.tcx.dcx().span_err(define.span, "item does not contain any opaque types");
190+
}
189191
self.mode = mode;
190192
}
191193
// Allow using `#[defines]` 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)