Skip to content

Commit

Permalink
Fix generic args specialization for unevaluated MIR constants (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsemakula authored Dec 3, 2024
1 parent b243ed7 commit 4b42ac4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion checker/src/block_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,7 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
) -> Rc<AbstractValue> {
let mut def_id = unevaluated.def;
let def_ty = self.bv.cv.tcx.type_of(def_id);
let args = self
let mut args = self
.type_visitor()
.specialize_generic_args(unevaluated.args, &self.type_visitor().generic_argument_map);
self.bv.cv.generic_args_cache.insert(def_id, args);
Expand All @@ -2751,7 +2751,10 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
rustc_middle::ty::Instance::resolve(self.bv.tcx, param_env, def_id, args)
{
def_id = instance.def.def_id();
args = instance.args;
trace!("resolved it to {:?}", def_id);
trace!("resolved args {:?}", args);
self.bv.cv.generic_args_cache.insert(def_id, args);
}
}
if self.bv.tcx.is_mir_available(def_id) {
Expand Down
2 changes: 1 addition & 1 deletion checker/src/body_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,10 @@ impl<'analysis, 'compilation, 'tcx> BodyVisitor<'analysis, 'compilation, 'tcx> {
}
}
self.reset_visitor_state();
*self.type_visitor_mut() = saved_type_visitor.clone();
}
self.def_id = saved_def_id;
self.mir = saved_mir;
*self.type_visitor_mut() = saved_type_visitor;
environment
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

// A test that visits `BlockVisitor::visit_unevaluated_const` with a non-promoted MIR constant
// that resolves to an instance with generic args.

pub trait MyTrait {
const MY_ASSOC_CONST: u8;
}

pub struct MyConstGenericImpl<const N: u8>;

impl<const N: u8> MyTrait for MyConstGenericImpl<N> {
const MY_ASSOC_CONST: u8 = N;
}

pub fn foo<T>(_x: T) {}

pub fn main() {
foo(MyConstGenericImpl::<1>::MY_ASSOC_CONST);
}

0 comments on commit 4b42ac4

Please sign in to comment.