From d8fb8b96144df7bf0b2066aca270e9597a67a2f2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 14 May 2024 13:53:35 +0200 Subject: [PATCH] Fix handling of unevaluated top-level constants --- frontend/exporter/src/constant_utils.rs | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/frontend/exporter/src/constant_utils.rs b/frontend/exporter/src/constant_utils.rs index 959dfb55a..698614328 100644 --- a/frontend/exporter/src/constant_utils.rs +++ b/frontend/exporter/src/constant_utils.rs @@ -353,23 +353,37 @@ pub trait ConstantExt<'tcx>: Sized + std::fmt::Debug { supposely_unreachable_fatal!(s, "TranslateUneval"; {self, ucv}); })) } else { - let cv = if let Some(assoc) = s.base().tcx.opt_associated_item(ucv.def) && - assoc.trait_item_def_id.is_some() { + let cv = if let Some(assoc) = s.base().tcx.opt_associated_item(ucv.def) { + if assoc.trait_item_def_id.is_some() { // This must be a trait declaration constant trait_const_to_constant_expr_kind(s, ucv.def, ucv.substs, &assoc) + } else { + // Constant appearing in an inherent impl block. + + // Solve the trait obligations + let parent_def_id = tcx.parent(ucv.def); + let param_env = tcx.param_env(s.owner_id()); + let trait_refs = + solve_item_traits(s, param_env, parent_def_id, ucv.substs, None); + + // Convert + let id = ucv.def.sinto(s); + let generics = ucv.substs.sinto(s); + ConstantExprKind::GlobalName { + id, + generics, + trait_refs, + } } - else { - // Top-level constant or a constant appearing in an impl block - - // Solve the trait obligations - let parent_def_id = tcx.parent(ucv.def); - let param_env = tcx.param_env(s.owner_id()); - let trait_refs = solve_item_traits(s, param_env, parent_def_id, ucv.substs, None); - - // Convert + } else { + // Top-level constant. + assert!(ucv.substs.is_empty(), "top-level constant has generics?"); let id = ucv.def.sinto(s); - let generics = ucv.substs.sinto(s); - ConstantExprKind::GlobalName { id, generics, trait_refs } + ConstantExprKind::GlobalName { + id, + generics: vec![], + trait_refs: vec![], + } }; TranslateUnevalRes::GlobalName(cv) }