Skip to content

Commit 1b7c3bc

Browse files
committed
allow special behavior when printing const infer
1 parent 11ec2a4 commit 1b7c3bc

File tree

6 files changed

+51
-18
lines changed

6 files changed

+51
-18
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
497497
let ty_to_string = |ty: Ty<'tcx>| -> String {
498498
let mut s = String::new();
499499
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
500-
let mut inner = self.inner.borrow_mut();
501-
let ty_vars = inner.type_variables();
502-
let getter = move |ty_vid| {
503-
let var_origin = ty_vars.var_origin(ty_vid);
504-
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind {
500+
let ty_getter = move |ty_vid| {
501+
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) =
502+
self.inner.borrow_mut().type_variables().var_origin(ty_vid).kind
503+
{
504+
Some(name.to_string())
505+
} else {
506+
None
507+
}
508+
};
509+
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
510+
let const_getter = move |ct_vid| {
511+
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = self
512+
.inner
513+
.borrow_mut()
514+
.const_unification_table()
515+
.probe_value(ct_vid)
516+
.origin
517+
.kind
518+
{
505519
return Some(name.to_string());
520+
} else {
521+
None
506522
}
507-
None
508523
};
509-
printer.name_resolver = Some(Box::new(&getter));
524+
printer.const_infer_name_resolver = Some(Box::new(const_getter));
525+
510526
let _ = if let ty::FnDef(..) = ty.kind() {
511527
// We don't want the regular output for `fn`s because it includes its path in
512528
// invalid pseudo-syntax, we want the `fn`-pointer output instead.

compiler/rustc_middle/src/ty/print/pretty.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ pub trait PrettyPrinter<'tcx>:
606606
ty::Infer(infer_ty) => {
607607
let verbose = self.tcx().sess.verbose();
608608
if let ty::TyVar(ty_vid) = infer_ty {
609-
if let Some(name) = self.infer_ty_name(ty_vid) {
609+
if let Some(name) = self.ty_infer_name(ty_vid) {
610610
p!(write("{}", name))
611611
} else {
612612
if verbose {
@@ -1015,7 +1015,11 @@ pub trait PrettyPrinter<'tcx>:
10151015
}
10161016
}
10171017

1018-
fn infer_ty_name(&self, _: ty::TyVid) -> Option<String> {
1018+
fn ty_infer_name(&self, _: ty::TyVid) -> Option<String> {
1019+
None
1020+
}
1021+
1022+
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<String> {
10191023
None
10201024
}
10211025

@@ -1203,7 +1207,14 @@ pub trait PrettyPrinter<'tcx>:
12031207
}
12041208
}
12051209
}
1206-
ty::ConstKind::Infer(..) => print_underscore!(),
1210+
ty::ConstKind::Infer(infer_ct) => {
1211+
match infer_ct {
1212+
ty::InferConst::Var(ct_vid)
1213+
if let Some(name) = self.const_infer_name(ct_vid) =>
1214+
p!(write("{}", name)),
1215+
_ => print_underscore!(),
1216+
}
1217+
}
12071218
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
12081219
ty::ConstKind::Value(value) => {
12091220
return self.pretty_print_const_value(value, ct.ty(), print_ty);
@@ -1551,7 +1562,8 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15511562

15521563
pub region_highlight_mode: RegionHighlightMode<'tcx>,
15531564

1554-
pub name_resolver: Option<Box<&'a dyn Fn(ty::TyVid) -> Option<String>>>,
1565+
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<String> + 'a>>,
1566+
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
15551567
}
15561568

15571569
impl<'a, 'tcx, F> Deref for FmtPrinter<'a, 'tcx, F> {
@@ -1580,7 +1592,8 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15801592
binder_depth: 0,
15811593
printed_type_count: 0,
15821594
region_highlight_mode: RegionHighlightMode::new(tcx),
1583-
name_resolver: None,
1595+
ty_infer_name_resolver: None,
1596+
const_infer_name_resolver: None,
15841597
}))
15851598
}
15861599
}
@@ -1835,8 +1848,12 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18351848
}
18361849

18371850
impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1838-
fn infer_ty_name(&self, id: ty::TyVid) -> Option<String> {
1839-
self.0.name_resolver.as_ref().and_then(|func| func(id))
1851+
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
1852+
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
1853+
}
1854+
1855+
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<String> {
1856+
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
18401857
}
18411858

18421859
fn print_value_path(

src/test/ui/const-generics/defaults/doesnt_infer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ impl<const N: u32> Foo<N> {
99
fn main() {
1010
let foo = Foo::<1>::foo();
1111
let foo = Foo::foo();
12-
//~^ error: type annotations needed for `Foo<{_: u32}>`
12+
//~^ error: type annotations needed for `Foo<N>`
1313
}

src/test/ui/const-generics/defaults/doesnt_infer.stderr

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

src/test/ui/const-generics/generic_arg_infer/issue-91614.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ use std::simd::Mask;
44

55
fn main() {
66
let y = Mask::<_, _>::splat(false);
7-
//~^ error: type annotations needed for `Mask<_, {_: usize}>`
7+
//~^ ERROR: type annotations needed for
88
}

src/test/ui/const-generics/generic_arg_infer/issue-91614.stderr

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

0 commit comments

Comments
 (0)