Skip to content

Commit 3f661da

Browse files
Add hir::ConstContext
1 parent 6318d24 commit 3f661da

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/librustc_hir/hir.rs

+47
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,53 @@ impl BodyOwnerKind {
12911291
}
12921292
}
12931293

1294+
/// The kind of an item that requires const-checking.
1295+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1296+
pub enum ConstContext {
1297+
/// A `const fn`.
1298+
ConstFn,
1299+
1300+
/// A `static` or `static mut`.
1301+
Static(Mutability),
1302+
1303+
/// A `const`, associated `const`, or other const context.
1304+
///
1305+
/// Other contexts include:
1306+
/// - Array length expressions
1307+
/// - Enum discriminants
1308+
/// - Const generics
1309+
///
1310+
/// For the most part, other contexts are treated just like a regular `const`, so they are
1311+
/// lumped into the same category.
1312+
Const,
1313+
}
1314+
1315+
impl ConstContext {
1316+
/// A description of this const context that can appear between backticks in an error message.
1317+
///
1318+
/// E.g. `const` or `static mut`.
1319+
pub fn keyword_name(self) -> &'static str {
1320+
match self {
1321+
Self::Const => "const",
1322+
Self::Static(Mutability::Not) => "static",
1323+
Self::Static(Mutability::Mut) => "static mut",
1324+
Self::ConstFn => "const fn",
1325+
}
1326+
}
1327+
}
1328+
1329+
/// A colloquial, trivially pluralizable description of this const context for use in error
1330+
/// messages.
1331+
impl fmt::Display for ConstContext {
1332+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1333+
match *self {
1334+
Self::Const => write!(f, "constant"),
1335+
Self::Static(_) => write!(f, "static"),
1336+
Self::ConstFn => write!(f, "constant function"),
1337+
}
1338+
}
1339+
}
1340+
12941341
/// A literal.
12951342
pub type Lit = Spanned<LitKind>;
12961343

src/librustc_middle/hir/map/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ impl<'hir> Map<'hir> {
408408
})
409409
}
410410

411+
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
412+
///
413+
/// Panics if `LocalDefId` does not have an associated body.
411414
pub fn body_owner_kind(&self, id: HirId) -> BodyOwnerKind {
412415
match self.get(id) {
413416
Node::Item(&Item { kind: ItemKind::Const(..), .. })
@@ -424,6 +427,23 @@ impl<'hir> Map<'hir> {
424427
}
425428
}
426429

430+
/// Returns the `ConstContext` of the body associated with this `LocalDefId`.
431+
///
432+
/// Panics if `LocalDefId` does not have an associated body.
433+
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
434+
let hir_id = self.local_def_id_to_hir_id(did);
435+
let ccx = match self.body_owner_kind(hir_id) {
436+
BodyOwnerKind::Const => ConstContext::Const,
437+
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
438+
439+
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
440+
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
441+
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
442+
};
443+
444+
Some(ccx)
445+
}
446+
427447
pub fn ty_param_owner(&self, id: HirId) -> HirId {
428448
match self.get(id) {
429449
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => id,

0 commit comments

Comments
 (0)