@@ -240,7 +240,7 @@ impl<'tcx> Const<'tcx> {
240240
241241 let ty = tcx. type_of ( def) . no_bound_vars ( ) . expect ( "const parameter types cannot be generic" ) ;
242242
243- match Self :: try_from_lit ( tcx, ty, expr) {
243+ match Self :: try_from_lit_or_param ( tcx, ty, expr) {
244244 Some ( v) => v,
245245 None => ty:: Const :: new_unevaluated (
246246 tcx,
@@ -281,7 +281,11 @@ impl<'tcx> Const<'tcx> {
281281 }
282282
283283 #[ instrument( skip( tcx) , level = "debug" ) ]
284- fn try_from_lit ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , expr : & ' tcx hir:: Expr < ' tcx > ) -> Option < Self > {
284+ fn try_from_lit_or_param (
285+ tcx : TyCtxt < ' tcx > ,
286+ ty : Ty < ' tcx > ,
287+ expr : & ' tcx hir:: Expr < ' tcx > ,
288+ ) -> Option < Self > {
285289 // Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
286290 // currently have to be wrapped in curly brackets, so it's necessary to special-case.
287291 let expr = match & expr. kind {
@@ -291,6 +295,22 @@ impl<'tcx> Const<'tcx> {
291295 _ => expr,
292296 } ;
293297
298+ if let hir:: ExprKind :: Path (
299+ qpath @ hir:: QPath :: Resolved (
300+ _,
301+ & hir:: Path { res : Res :: Def ( DefKind :: ConstParam , _) , .. } ,
302+ ) ,
303+ ) = expr. kind
304+ {
305+ if tcx. features ( ) . const_arg_path {
306+ span_bug ! (
307+ expr. span,
308+ "try_from_lit: received const param which shouldn't be possible"
309+ ) ;
310+ }
311+ return Some ( Const :: from_param ( tcx, qpath, expr. hir_id ) ) ;
312+ } ;
313+
294314 let lit_input = match expr. kind {
295315 hir:: ExprKind :: Lit ( lit) => Some ( LitToConstInput { lit : & lit. node , ty, neg : false } ) ,
296316 hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , expr) => match expr. kind {
@@ -318,14 +338,6 @@ impl<'tcx> Const<'tcx> {
318338 }
319339 }
320340
321- if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved (
322- _,
323- & hir:: Path { res : Res :: Def ( DefKind :: ConstParam , _) , .. } ,
324- ) ) = expr. kind
325- {
326- span_bug ! ( expr. span, "try_from_lit: received const param which shouldn't be possible" )
327- }
328-
329341 None
330342 }
331343
0 commit comments