Skip to content

Commit a59eb6d

Browse files
committed
Pretty print function pointer const values.
1 parent 2afd277 commit a59eb6d

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

src/librustc/mir/interpret/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ impl<'tcx> AllocMap<'tcx> {
470470
}
471471
}
472472

473+
/// Panics if the `AllocId` does not refer to a function
474+
pub fn unwrap_fn(&self, id: AllocId) -> Instance<'tcx> {
475+
match self.get(id) {
476+
Some(GlobalAlloc::Function(instance)) => instance,
477+
_ => bug!("expected allocation ID {} to point to a function", id),
478+
}
479+
}
480+
473481
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
474482
/// call this function twice, even with the same `Allocation` will ICE the compiler.
475483
pub fn set_alloc_id_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {

src/librustc/ty/print/pretty.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,18 @@ pub trait PrettyPrinter<'tcx>:
980980
return Ok(self);
981981
}
982982
}
983+
984+
if let ty::FnPtr(_) = ct.ty.kind {
985+
if let ConstValue::Scalar(Scalar::Ptr(ptr)) = ct.val {
986+
let instance = {
987+
let alloc_map = self.tcx().alloc_map.lock();
988+
alloc_map.unwrap_fn(ptr.alloc_id)
989+
};
990+
p!(print_value_path(instance.def_id(), instance.substs));
991+
return Ok(self);
992+
}
993+
}
994+
983995
p!(write("{:?} : ", ct.val), print(ct.ty));
984996

985997
Ok(self)

src/librustc/ty/relate.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::hir::def_id::DefId;
88
use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
99
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
1010
use crate::ty::error::{ExpectedFound, TypeError};
11-
use crate::mir::interpret::{ConstValue, get_slice_bytes, Scalar, GlobalAlloc};
11+
use crate::mir::interpret::{ConstValue, get_slice_bytes};
1212
use std::rc::Rc;
1313
use std::iter;
1414
use rustc_target::spec::abi;
@@ -577,16 +577,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
577577
Ok(ConstValue::Scalar(a_val))
578578
} else if let ty::FnPtr(_) = a.ty.kind {
579579
let alloc_map = tcx.alloc_map.lock();
580-
let get_fn_instance = |val: Scalar| {
581-
let ptr = val.to_ptr().unwrap();
582-
if let Some(GlobalAlloc::Function(instance)) = alloc_map.get(ptr.alloc_id) {
583-
instance
584-
} else {
585-
bug!("Allocation for FnPtr isn't a function");
586-
}
587-
};
588-
let a_instance = get_fn_instance(a_val);
589-
let b_instance = get_fn_instance(b_val);
580+
let a_instance = alloc_map.unwrap_fn(a_val.to_ptr().unwrap().alloc_id);
581+
let b_instance = alloc_map.unwrap_fn(b_val.to_ptr().unwrap().alloc_id);
590582
if a_instance == b_instance {
591583
Ok(ConstValue::Scalar(a_val))
592584
} else {

src/test/ui/const-generics/fn-const-param-call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ impl<const F: fn() -> u32> Wrapper<{F}> {
1717

1818
fn main() {
1919
assert_eq!(Wrapper::<{function}>::call(), 17);
20-
}
20+
}

src/test/ui/const-generics/fn-const-param-infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ fn main() {
2323
let _ = Checked::<{generic::<u16>}>;
2424
let _: Checked<{generic::<u16>}> = Checked::<{generic::<u16>}>;
2525
let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>; //~ mismatched types
26-
}
26+
}

src/test/ui/const-generics/fn-const-param-infer.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0308]: mismatched types
1010
--> $DIR/fn-const-param-infer.rs:16:33
1111
|
1212
LL | let _: Checked<{not_one}> = Checked::<{not_two}>;
13-
| ^^^^^^^^^^^^^^^^^^^^ expected `Scalar(AllocId(1).0x0) : fn(usize) -> bool`, found `Scalar(AllocId(10).0x0) : fn(usize) -> bool`
13+
| ^^^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
1414
|
1515
= note: expected type `Checked<>`
1616
found type `Checked<>`
@@ -34,7 +34,7 @@ error[E0308]: mismatched types
3434
--> $DIR/fn-const-param-infer.rs:25:40
3535
|
3636
LL | let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>;
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Scalar(AllocId(7).0x0) : fn(usize) -> bool`, found `Scalar(AllocId(20).0x0) : fn(usize) -> bool`
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
3838
|
3939
= note: expected type `Checked<>`
4040
found type `Checked<>`

0 commit comments

Comments
 (0)