allow special behavior when printing const infer

This commit is contained in:
lcnr 2022-02-14 14:13:02 +01:00
parent 11ec2a47a4
commit 1b7c3bcef9
6 changed files with 52 additions and 19 deletions

View File

@ -497,16 +497,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let ty_to_string = |ty: Ty<'tcx>| -> String {
let mut s = String::new();
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
let mut inner = self.inner.borrow_mut();
let ty_vars = inner.type_variables();
let getter = move |ty_vid| {
let var_origin = ty_vars.var_origin(ty_vid);
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind {
return Some(name.to_string());
let ty_getter = move |ty_vid| {
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) =
self.inner.borrow_mut().type_variables().var_origin(ty_vid).kind
{
Some(name.to_string())
} else {
None
}
None
};
printer.name_resolver = Some(Box::new(&getter));
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
let const_getter = move |ct_vid| {
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = self
.inner
.borrow_mut()
.const_unification_table()
.probe_value(ct_vid)
.origin
.kind
{
return Some(name.to_string());
} else {
None
}
};
printer.const_infer_name_resolver = Some(Box::new(const_getter));
let _ = if let ty::FnDef(..) = ty.kind() {
// We don't want the regular output for `fn`s because it includes its path in
// invalid pseudo-syntax, we want the `fn`-pointer output instead.

View File

@ -606,7 +606,7 @@ pub trait PrettyPrinter<'tcx>:
ty::Infer(infer_ty) => {
let verbose = self.tcx().sess.verbose();
if let ty::TyVar(ty_vid) = infer_ty {
if let Some(name) = self.infer_ty_name(ty_vid) {
if let Some(name) = self.ty_infer_name(ty_vid) {
p!(write("{}", name))
} else {
if verbose {
@ -1015,7 +1015,11 @@ pub trait PrettyPrinter<'tcx>:
}
}
fn infer_ty_name(&self, _: ty::TyVid) -> Option<String> {
fn ty_infer_name(&self, _: ty::TyVid) -> Option<String> {
None
}
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<String> {
None
}
@ -1203,7 +1207,14 @@ pub trait PrettyPrinter<'tcx>:
}
}
}
ty::ConstKind::Infer(..) => print_underscore!(),
ty::ConstKind::Infer(infer_ct) => {
match infer_ct {
ty::InferConst::Var(ct_vid)
if let Some(name) = self.const_infer_name(ct_vid) =>
p!(write("{}", name)),
_ => print_underscore!(),
}
}
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
ty::ConstKind::Value(value) => {
return self.pretty_print_const_value(value, ct.ty(), print_ty);
@ -1551,7 +1562,8 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
pub region_highlight_mode: RegionHighlightMode<'tcx>,
pub name_resolver: Option<Box<&'a dyn Fn(ty::TyVid) -> Option<String>>>,
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<String> + 'a>>,
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
}
impl<'a, 'tcx, F> Deref for FmtPrinter<'a, 'tcx, F> {
@ -1580,7 +1592,8 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
binder_depth: 0,
printed_type_count: 0,
region_highlight_mode: RegionHighlightMode::new(tcx),
name_resolver: None,
ty_infer_name_resolver: None,
const_infer_name_resolver: None,
}))
}
}
@ -1835,8 +1848,12 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
}
impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
fn infer_ty_name(&self, id: ty::TyVid) -> Option<String> {
self.0.name_resolver.as_ref().and_then(|func| func(id))
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
}
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<String> {
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
}
fn print_value_path(

View File

@ -9,5 +9,5 @@ impl<const N: u32> Foo<N> {
fn main() {
let foo = Foo::<1>::foo();
let foo = Foo::foo();
//~^ error: type annotations needed for `Foo<{_: u32}>`
//~^ error: type annotations needed for `Foo<N>`
}

View File

@ -1,4 +1,4 @@
error[E0282]: type annotations needed for `Foo<{_: u32}>`
error[E0282]: type annotations needed for `Foo<N>`
--> $DIR/doesnt_infer.rs:11:15
|
LL | let foo = Foo::foo();

View File

@ -4,5 +4,5 @@ use std::simd::Mask;
fn main() {
let y = Mask::<_, _>::splat(false);
//~^ error: type annotations needed for `Mask<_, {_: usize}>`
//~^ ERROR: type annotations needed for
}

View File

@ -1,4 +1,4 @@
error[E0283]: type annotations needed for `Mask<_, {_: usize}>`
error[E0283]: type annotations needed for `Mask<_, LANES>`
--> $DIR/issue-91614.rs:6:13
|
LL | let y = Mask::<_, _>::splat(false);