Skip to content

Commit a385e56

Browse files
committed
Auto merge of rust-lang#122392 - BoxyUwU:misc_cleanup, r=lcnr
misc cleanups from debugging something rename `instantiate_canonical_with_fresh_inference_vars` to `instantiate_canonical` the substs for the canonical are not solely infer vars as that would be wildly wrong and it is rather confusing to see this method called and think that the entire canonicalization setup is completely broken when it is not 👍 also update region debug printing to be more like the custom impls for Ty/Const, right now regions in debug output are horribly verbose and make it incredibly hard to read but with this atleast boundvars and placeholders when debugging the new solver do not take up excessive amounts of space. r? `@lcnr`
2 parents 200e3f7 + e34e344 commit a385e56

38 files changed

+205
-173
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
6161
Ok(output)
6262
}
6363

64-
pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
64+
pub(super) fn instantiate_canonical<T>(
6565
&mut self,
6666
span: Span,
6767
canonical: &Canonical<'tcx, T>,
6868
) -> T
6969
where
7070
T: TypeFoldable<TyCtxt<'tcx>>,
7171
{
72-
let (instantiated, _) =
73-
self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
72+
let (instantiated, _) = self.infcx.instantiate_canonical(span, canonical);
7473
instantiated
7574
}
7675

compiler/rustc_borrowck/src/type_check/input_output.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
3939
// (e.g., the `_` in the code above) with fresh variables.
4040
// Then replace the bound items in the fn sig with fresh variables,
4141
// so that they represent the view from "inside" the closure.
42-
let user_provided_sig = self
43-
.instantiate_canonical_with_fresh_inference_vars(body.span, &user_provided_poly_sig);
42+
let user_provided_sig = self.instantiate_canonical(body.span, &user_provided_poly_sig);
4443
let mut user_provided_sig = self.infcx.instantiate_binder_with_fresh_vars(
4544
body.span,
4645
BoundRegionConversionTime::FnCall,

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11091109
let tcx = self.tcx();
11101110
for user_annotation in self.user_type_annotations {
11111111
let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
1112-
let annotation = self.instantiate_canonical_with_fresh_inference_vars(span, user_ty);
1112+
let annotation = self.instantiate_canonical(span, user_ty);
11131113
if let ty::UserType::TypeOf(def, args) = annotation
11141114
&& let DefKind::InlineConst = tcx.def_kind(def)
11151115
{

compiler/rustc_hir_typeck/src/method/probe.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
386386

387387
let infcx = &self.infcx;
388388
let (ParamEnvAnd { param_env: _, value: self_ty }, canonical_inference_vars) =
389-
infcx.instantiate_canonical_with_fresh_inference_vars(
390-
span,
391-
&param_env_and_self_ty,
392-
);
389+
infcx.instantiate_canonical(span, &param_env_and_self_ty);
393390
debug!(
394391
"probe_op: Mode::Path, param_env_and_self_ty={:?} self_ty={:?}",
395392
param_env_and_self_ty, self_ty
@@ -661,13 +658,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
661658
// of the iterations in the autoderef loop, so there is no problem with it
662659
// being discoverable in another one of these iterations.
663660
//
664-
// Using `instantiate_canonical_with_fresh_inference_vars` on our
661+
// Using `instantiate_canonical` on our
665662
// `Canonical<QueryResponse<Ty<'tcx>>>` and then *throwing away* the
666663
// `CanonicalVarValues` will exactly give us such a generalization - it
667664
// will still match the original object type, but it won't pollute our
668665
// type variables in any form, so just do that!
669666
let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
670-
self.fcx.instantiate_canonical_with_fresh_inference_vars(self.span, self_ty);
667+
self.fcx.instantiate_canonical(self.span, self_ty);
671668

672669
self.assemble_inherent_candidates_from_object(generalized_self_ty);
673670
self.assemble_inherent_impl_candidates_for_type(p.def_id());

compiler/rustc_infer/src/infer/canonical/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ mod instantiate;
3838
pub mod query_response;
3939

4040
impl<'tcx> InferCtxt<'tcx> {
41-
/// Creates an instantiation S for the canonical value with fresh
42-
/// inference variables and applies it to the canonical value.
41+
/// Creates an instantiation S for the canonical value with fresh inference
42+
/// variables and placeholders then applies it to the canonical value.
4343
/// Returns both the instantiated result *and* the instantiation S.
4444
///
4545
/// This can be invoked as part of constructing an
@@ -50,7 +50,7 @@ impl<'tcx> InferCtxt<'tcx> {
5050
/// At the end of processing, the instantiation S (once
5151
/// canonicalized) then represents the values that you computed
5252
/// for each of the canonical inputs to your query.
53-
pub fn instantiate_canonical_with_fresh_inference_vars<T>(
53+
pub fn instantiate_canonical<T>(
5454
&self,
5555
span: Span,
5656
canonical: &Canonical<'tcx, T>,

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
678678
T: TypeFoldable<TyCtxt<'tcx>>,
679679
{
680680
let infcx = self.build();
681-
let (value, args) = infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
681+
let (value, args) = infcx.instantiate_canonical(span, canonical);
682682
(infcx, value, args)
683683
}
684684

compiler/rustc_middle/src/ty/region.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ pub struct EarlyParamRegion {
336336

337337
impl std::fmt::Debug for EarlyParamRegion {
338338
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
339-
write!(f, "{:?}, {}, {}", self.def_id, self.index, self.name)
339+
// FIXME(BoxyUwU): self.def_id goes first because of `erased-regions-in-hidden-ty.rs` being impossible to write
340+
// error annotations for otherwise. :). Ideally this would be `self.name, self.index, self.def_id`.
341+
write!(f, "{:?}_{}/#{}", self.def_id, self.name, self.index)
340342
}
341343
}
342344

@@ -381,13 +383,25 @@ pub enum BoundRegionKind {
381383
BrEnv,
382384
}
383385

384-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, PartialOrd, Ord)]
386+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, PartialOrd, Ord)]
385387
#[derive(HashStable)]
386388
pub struct BoundRegion {
387389
pub var: BoundVar,
388390
pub kind: BoundRegionKind,
389391
}
390392

393+
impl core::fmt::Debug for BoundRegion {
394+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
395+
match self.kind {
396+
BoundRegionKind::BrAnon => write!(f, "{:?}", self.var),
397+
BoundRegionKind::BrEnv => write!(f, "{:?}.Env", self.var),
398+
BoundRegionKind::BrNamed(def, symbol) => {
399+
write!(f, "{:?}.Named({:?}, {:?})", self.var, def, symbol)
400+
}
401+
}
402+
}
403+
}
404+
391405
impl BoundRegionKind {
392406
pub fn is_named(&self) -> bool {
393407
match *self {

compiler/rustc_type_ir/src/region_kind.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,27 @@ impl<I: Interner> DebugWithInfcx<I> for RegionKind<I> {
223223
f: &mut core::fmt::Formatter<'_>,
224224
) -> core::fmt::Result {
225225
match this.data {
226-
ReEarlyParam(data) => write!(f, "ReEarlyParam({data:?})"),
226+
ReEarlyParam(data) => write!(f, "{data:?}"),
227227

228228
ReBound(binder_id, bound_region) => {
229-
write!(f, "ReBound({binder_id:?}, {bound_region:?})")
229+
write!(f, "'")?;
230+
crate::debug_bound_var(f, *binder_id, bound_region)
230231
}
231232

232233
ReLateParam(fr) => write!(f, "{fr:?}"),
233234

234-
ReStatic => f.write_str("ReStatic"),
235+
ReStatic => f.write_str("'static"),
235236

236237
ReVar(vid) => write!(f, "{:?}", &this.wrap(vid)),
237238

238-
RePlaceholder(placeholder) => write!(f, "RePlaceholder({placeholder:?})"),
239+
RePlaceholder(placeholder) => write!(f, "{placeholder:?}"),
239240

240-
ReErased => f.write_str("ReErased"),
241+
// Use `'{erased}` as the output instead of `'erased` so that its more obviously distinct from
242+
// a `ReEarlyParam` named `'erased`. Technically that would print as `'erased/#IDX` so this is
243+
// not strictly necessary but *shrug*
244+
ReErased => f.write_str("'{erased}"),
241245

242-
ReError(_) => f.write_str("ReError"),
246+
ReError(_) => f.write_str("'{region error}"),
243247
}
244248
}
245249
}

tests/mir-opt/issue_99325.main.built.after.32bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| User Type Annotations
44
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [&*b"AAAA"], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5-
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[d56d]::main::{constant#1}), args: [] }: &ReStatic [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:14:16: 14:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5+
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[d56d]::main::{constant#1}), args: [] }: &'static [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:14:16: 14:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
66
|
77
fn main() -> () {
88
let mut _0: ();

tests/mir-opt/issue_99325.main.built.after.64bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| User Type Annotations
44
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [&*b"AAAA"], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5-
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[d56d]::main::{constant#1}), args: [] }: &ReStatic [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:14:16: 14:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5+
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[d56d]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[d56d]::main::{constant#1}), args: [] }: &'static [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:14:16: 14:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
66
|
77
fn main() -> () {
88
let mut _0: ();

tests/ui/associated-types/substs-ppaux.normal.stderr

+42-36
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,73 @@
11
error[E0308]: mismatched types
2-
--> $DIR/substs-ppaux.rs:16:17
2+
--> $DIR/substs-ppaux.rs:23:17
33
|
4-
LL | fn bar<'a, T>() where T: 'a {}
5-
| --------------------------- associated function `bar` defined here
4+
LL | / fn bar<'a, T>()
5+
LL | | where
6+
LL | | T: 'a,
7+
| |______________- associated function `bar` defined here
68
...
7-
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
8-
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
9-
| |
10-
| expected due to this
9+
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
10+
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
11+
| |
12+
| expected due to this
1113
|
1214
= note: expected unit type `()`
1315
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
1416
help: use parentheses to call this associated function
1517
|
16-
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
17-
| ++
18+
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
19+
| ++
1820

1921
error[E0308]: mismatched types
20-
--> $DIR/substs-ppaux.rs:25:17
22+
--> $DIR/substs-ppaux.rs:31:17
2123
|
22-
LL | fn bar<'a, T>() where T: 'a {}
23-
| --------------------------- associated function `bar` defined here
24+
LL | / fn bar<'a, T>()
25+
LL | | where
26+
LL | | T: 'a,
27+
| |______________- associated function `bar` defined here
2428
...
25-
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
26-
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
27-
| |
28-
| expected due to this
29+
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
30+
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
31+
| |
32+
| expected due to this
2933
|
3034
= note: expected unit type `()`
3135
found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
3236
help: use parentheses to call this associated function
3337
|
34-
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
35-
| ++
38+
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
39+
| ++
3640

3741
error[E0308]: mismatched types
38-
--> $DIR/substs-ppaux.rs:33:17
42+
--> $DIR/substs-ppaux.rs:39:17
3943
|
4044
LL | fn baz() {}
4145
| -------- associated function `baz` defined here
4246
...
43-
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
44-
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
47+
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
48+
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
4549
| |
4650
| expected due to this
4751
|
4852
= note: expected unit type `()`
4953
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
5054
help: use parentheses to call this associated function
5155
|
52-
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
53-
| ++
56+
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
57+
| ++
5458

5559
error[E0308]: mismatched types
56-
--> $DIR/substs-ppaux.rs:41:17
60+
--> $DIR/substs-ppaux.rs:47:17
5761
|
58-
LL | fn foo<'z>() where &'z (): Sized {
59-
| -------------------------------- function `foo` defined here
62+
LL | / fn foo<'z>()
63+
LL | | where
64+
LL | | &'z (): Sized,
65+
| |__________________- function `foo` defined here
6066
...
61-
LL | let x: () = foo::<'static>;
62-
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item
63-
| |
64-
| expected due to this
67+
LL | let x: () = foo::<'static>;
68+
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item
69+
| |
70+
| expected due to this
6571
|
6672
= note: expected unit type `()`
6773
found fn item `fn() {foo::<'static>}`
@@ -71,18 +77,18 @@ LL | let x: () = foo::<'static>();
7177
| ++
7278

7379
error[E0277]: the trait bound `str: Foo<'_, '_, u8>` is not satisfied
74-
--> $DIR/substs-ppaux.rs:49:6
80+
--> $DIR/substs-ppaux.rs:55:6
7581
|
7682
LL | <str as Foo<u8>>::bar;
7783
| ^^^ the trait `Sized` is not implemented for `str`, which is required by `str: Foo<'_, '_, u8>`
7884
|
7985
note: required for `str` to implement `Foo<'_, '_, u8>`
80-
--> $DIR/substs-ppaux.rs:11:17
86+
--> $DIR/substs-ppaux.rs:15:20
8187
|
82-
LL | impl<'a,'b,T,S> Foo<'a, 'b, S> for T {}
83-
| - ^^^^^^^^^^^^^^ ^
84-
| |
85-
| unsatisfied trait bound introduced here
88+
LL | impl<'a, 'b, T, S> Foo<'a, 'b, S> for T {}
89+
| - ^^^^^^^^^^^^^^ ^
90+
| |
91+
| unsatisfied trait bound introduced here
8692

8793
error: aborting due to 5 previous errors
8894

tests/ui/associated-types/substs-ppaux.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,51 @@
33
//
44
//@[verbose] compile-flags: -Z verbose-internals
55

6-
trait Foo<'b, 'c, S=u32> {
7-
fn bar<'a, T>() where T: 'a {}
6+
trait Foo<'b, 'c, S = u32> {
7+
fn bar<'a, T>()
8+
where
9+
T: 'a,
10+
{
11+
}
812
fn baz() {}
913
}
1014

11-
impl<'a,'b,T,S> Foo<'a, 'b, S> for T {}
15+
impl<'a, 'b, T, S> Foo<'a, 'b, S> for T {}
1216

1317
fn main() {}
1418

15-
fn foo<'z>() where &'z (): Sized {
16-
let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
19+
fn foo<'z>()
20+
where
21+
&'z (): Sized,
22+
{
23+
let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
1724
//[verbose]~^ ERROR mismatched types
1825
//[verbose]~| expected unit type `()`
19-
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>}`
26+
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
2027
//[normal]~^^^^ ERROR mismatched types
2128
//[normal]~| expected unit type `()`
2229
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
2330

24-
25-
let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
31+
let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
2632
//[verbose]~^ ERROR mismatched types
2733
//[verbose]~| expected unit type `()`
28-
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
34+
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
2935
//[normal]~^^^^ ERROR mismatched types
3036
//[normal]~| expected unit type `()`
3137
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
3238

33-
let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
39+
let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
3440
//[verbose]~^ ERROR mismatched types
3541
//[verbose]~| expected unit type `()`
36-
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz}`
42+
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
3743
//[normal]~^^^^ ERROR mismatched types
3844
//[normal]~| expected unit type `()`
3945
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
4046

4147
let x: () = foo::<'static>;
4248
//[verbose]~^ ERROR mismatched types
4349
//[verbose]~| expected unit type `()`
44-
//[verbose]~| found fn item `fn() {foo::<ReStatic>}`
50+
//[verbose]~| found fn item `fn() {foo::<'static>}`
4551
//[normal]~^^^^ ERROR mismatched types
4652
//[normal]~| expected unit type `()`
4753
//[normal]~| found fn item `fn() {foo::<'static>}`

0 commit comments

Comments
 (0)