@@ -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.
12951342pub type Lit = Spanned < LitKind > ;
12961343
0 commit comments