Skip to content

Commit 78e4456

Browse files
authored
Rollup merge of rust-lang#93990 - lcnr:pre-89862-cleanup, r=estebank
pre rust-lang#89862 cleanup changes used in rust-lang#89862 which can be landed without the rest of this PR being finished. r? `@estebank`
2 parents 26dd6ac + 1b7c3bc commit 78e4456

File tree

8 files changed

+88
-54
lines changed

8 files changed

+88
-54
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);
@@ -1559,7 +1570,8 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15591570

15601571
pub region_highlight_mode: RegionHighlightMode<'tcx>,
15611572

1562-
pub name_resolver: Option<Box<&'a dyn Fn(ty::TyVid) -> Option<String>>>,
1573+
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<String> + 'a>>,
1574+
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
15631575
}
15641576

15651577
impl<'a, 'tcx, F> Deref for FmtPrinter<'a, 'tcx, F> {
@@ -1588,7 +1600,8 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15881600
binder_depth: 0,
15891601
printed_type_count: 0,
15901602
region_highlight_mode: RegionHighlightMode::new(tcx),
1591-
name_resolver: None,
1603+
ty_infer_name_resolver: None,
1604+
const_infer_name_resolver: None,
15921605
}))
15931606
}
15941607
}
@@ -1843,8 +1856,12 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18431856
}
18441857

18451858
impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1846-
fn infer_ty_name(&self, id: ty::TyVid) -> Option<String> {
1847-
self.0.name_resolver.as_ref().and_then(|func| func(id))
1859+
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
1860+
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
1861+
}
1862+
1863+
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<String> {
1864+
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
18481865
}
18491866

18501867
fn print_value_path(

compiler/rustc_middle/src/ty/util.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_data_structures::intern::Interned;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_errors::ErrorReported;
1919
use rustc_hir as hir;
20-
use rustc_hir::def::DefKind;
20+
use rustc_hir::def::{CtorOf, DefKind, Res};
2121
use rustc_hir::def_id::DefId;
2222
use rustc_macros::HashStable;
2323
use rustc_query_system::ich::NodeIdHashingMode;
@@ -146,6 +146,37 @@ impl<'tcx> TyCtxt<'tcx> {
146146
hasher.finish()
147147
}
148148

149+
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
150+
match res {
151+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
152+
Some(self.parent(def_id).and_then(|def_id| self.parent(def_id)).unwrap())
153+
}
154+
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
155+
Some(self.parent(def_id).unwrap())
156+
}
157+
// Other `DefKind`s don't have generics and would ICE when calling
158+
// `generics_of`.
159+
Res::Def(
160+
DefKind::Struct
161+
| DefKind::Union
162+
| DefKind::Enum
163+
| DefKind::Trait
164+
| DefKind::OpaqueTy
165+
| DefKind::TyAlias
166+
| DefKind::ForeignTy
167+
| DefKind::TraitAlias
168+
| DefKind::AssocTy
169+
| DefKind::Fn
170+
| DefKind::AssocFn
171+
| DefKind::AssocConst
172+
| DefKind::Impl,
173+
def_id,
174+
) => Some(def_id),
175+
Res::Err => None,
176+
_ => None,
177+
}
178+
}
179+
149180
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
150181
if let ty::Adt(def, substs) = *ty.kind() {
151182
for field in def.all_fields() {

compiler/rustc_typeck/src/collect/type_of.rs

+5-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use rustc_errors::{Applicability, ErrorReported, StashKey};
22
use rustc_hir as hir;
3-
use rustc_hir::def::CtorOf;
4-
use rustc_hir::def::{DefKind, Res};
3+
use rustc_hir::def::Res;
54
use rustc_hir::def_id::{DefId, LocalDefId};
65
use rustc_hir::intravisit;
76
use rustc_hir::intravisit::Visitor;
87
use rustc_hir::{HirId, Node};
98
use rustc_middle::hir::nested_filter;
109
use rustc_middle::ty::subst::InternalSubsts;
1110
use rustc_middle::ty::util::IntTypeExt;
12-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeFolder};
11+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder};
1312
use rustc_span::symbol::Ident;
1413
use rustc_span::{Span, DUMMY_SP};
1514

@@ -198,38 +197,9 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
198197
// Try to use the segment resolution if it is valid, otherwise we
199198
// default to the path resolution.
200199
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
201-
let generics = match res {
202-
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx
203-
.generics_of(tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap()),
204-
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
205-
tcx.generics_of(tcx.parent(def_id).unwrap())
206-
}
207-
// Other `DefKind`s don't have generics and would ICE when calling
208-
// `generics_of`.
209-
Res::Def(
210-
DefKind::Struct
211-
| DefKind::Union
212-
| DefKind::Enum
213-
| DefKind::Trait
214-
| DefKind::OpaqueTy
215-
| DefKind::TyAlias
216-
| DefKind::ForeignTy
217-
| DefKind::TraitAlias
218-
| DefKind::AssocTy
219-
| DefKind::Fn
220-
| DefKind::AssocFn
221-
| DefKind::AssocConst
222-
| DefKind::Impl,
223-
def_id,
224-
) => tcx.generics_of(def_id),
225-
Res::Err => {
226-
tcx.sess.delay_span_bug(tcx.def_span(def_id), "anon const with Res::Err");
227-
return None;
228-
}
229-
_ => {
230-
// If the user tries to specify generics on a type that does not take them,
231-
// e.g. `usize<T>`, we may hit this branch, in which case we treat it as if
232-
// no arguments have been passed. An error should already have been emitted.
200+
let generics = match tcx.res_generics_def_id(res) {
201+
Some(def_id) => tcx.generics_of(def_id),
202+
None => {
233203
tcx.sess.delay_span_bug(
234204
tcx.def_span(def_id),
235205
&format!("unexpected anon const res {:?} in path: {:?}", res, 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)