Skip to content

Commit

Permalink
Auto merge of #122392 - BoxyUwU:misc_cleanup, r=lcnr
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Mar 19, 2024
2 parents 200e3f7 + e34e344 commit a385e56
Show file tree
Hide file tree
Showing 38 changed files with 205 additions and 173 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Ok(output)
}

pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
pub(super) fn instantiate_canonical<T>(
&mut self,
span: Span,
canonical: &Canonical<'tcx, T>,
) -> T
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
let (instantiated, _) =
self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
let (instantiated, _) = self.infcx.instantiate_canonical(span, canonical);
instantiated
}

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/type_check/input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// (e.g., the `_` in the code above) with fresh variables.
// Then replace the bound items in the fn sig with fresh variables,
// so that they represent the view from "inside" the closure.
let user_provided_sig = self
.instantiate_canonical_with_fresh_inference_vars(body.span, &user_provided_poly_sig);
let user_provided_sig = self.instantiate_canonical(body.span, &user_provided_poly_sig);
let mut user_provided_sig = self.infcx.instantiate_binder_with_fresh_vars(
body.span,
BoundRegionConversionTime::FnCall,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let tcx = self.tcx();
for user_annotation in self.user_type_annotations {
let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
let annotation = self.instantiate_canonical_with_fresh_inference_vars(span, user_ty);
let annotation = self.instantiate_canonical(span, user_ty);
if let ty::UserType::TypeOf(def, args) = annotation
&& let DefKind::InlineConst = tcx.def_kind(def)
{
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

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

self.assemble_inherent_candidates_from_object(generalized_self_ty);
self.assemble_inherent_impl_candidates_for_type(p.def_id());
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_infer/src/infer/canonical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ mod instantiate;
pub mod query_response;

impl<'tcx> InferCtxt<'tcx> {
/// Creates an instantiation S for the canonical value with fresh
/// inference variables and applies it to the canonical value.
/// Creates an instantiation S for the canonical value with fresh inference
/// variables and placeholders then applies it to the canonical value.
/// Returns both the instantiated result *and* the instantiation S.
///
/// This can be invoked as part of constructing an
Expand All @@ -50,7 +50,7 @@ impl<'tcx> InferCtxt<'tcx> {
/// At the end of processing, the instantiation S (once
/// canonicalized) then represents the values that you computed
/// for each of the canonical inputs to your query.
pub fn instantiate_canonical_with_fresh_inference_vars<T>(
pub fn instantiate_canonical<T>(
&self,
span: Span,
canonical: &Canonical<'tcx, T>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
T: TypeFoldable<TyCtxt<'tcx>>,
{
let infcx = self.build();
let (value, args) = infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
let (value, args) = infcx.instantiate_canonical(span, canonical);
(infcx, value, args)
}

Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_middle/src/ty/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ pub struct EarlyParamRegion {

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

Expand Down Expand Up @@ -381,13 +383,25 @@ pub enum BoundRegionKind {
BrEnv,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, PartialOrd, Ord)]
#[derive(HashStable)]
pub struct BoundRegion {
pub var: BoundVar,
pub kind: BoundRegionKind,
}

impl core::fmt::Debug for BoundRegion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.kind {
BoundRegionKind::BrAnon => write!(f, "{:?}", self.var),
BoundRegionKind::BrEnv => write!(f, "{:?}.Env", self.var),
BoundRegionKind::BrNamed(def, symbol) => {
write!(f, "{:?}.Named({:?}, {:?})", self.var, def, symbol)
}
}
}
}

impl BoundRegionKind {
pub fn is_named(&self) -> bool {
match *self {
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_type_ir/src/region_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,27 @@ impl<I: Interner> DebugWithInfcx<I> for RegionKind<I> {
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
match this.data {
ReEarlyParam(data) => write!(f, "ReEarlyParam({data:?})"),
ReEarlyParam(data) => write!(f, "{data:?}"),

ReBound(binder_id, bound_region) => {
write!(f, "ReBound({binder_id:?}, {bound_region:?})")
write!(f, "'")?;
crate::debug_bound_var(f, *binder_id, bound_region)
}

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

ReStatic => f.write_str("ReStatic"),
ReStatic => f.write_str("'static"),

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

RePlaceholder(placeholder) => write!(f, "RePlaceholder({placeholder:?})"),
RePlaceholder(placeholder) => write!(f, "{placeholder:?}"),

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

ReError(_) => f.write_str("ReError"),
ReError(_) => f.write_str("'{region error}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/issue_99325.main.built.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| User Type Annotations
| 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">}
| 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">}
| 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">}
|
fn main() -> () {
let mut _0: ();
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/issue_99325.main.built.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| User Type Annotations
| 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">}
| 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">}
| 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">}
|
fn main() -> () {
let mut _0: ();
Expand Down
78 changes: 42 additions & 36 deletions tests/ui/associated-types/substs-ppaux.normal.stderr
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:16:17
--> $DIR/substs-ppaux.rs:23:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- associated function `bar` defined here
LL | / fn bar<'a, T>()
LL | | where
LL | | T: 'a,
| |______________- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
| ++
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
| ++

error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:25:17
--> $DIR/substs-ppaux.rs:31:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- associated function `bar` defined here
LL | / fn bar<'a, T>()
LL | | where
LL | | T: 'a,
| |______________- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
| ++
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
| ++

error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:33:17
--> $DIR/substs-ppaux.rs:39:17
|
LL | fn baz() {}
| -------- associated function `baz` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
| ++
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
| ++

error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:41:17
--> $DIR/substs-ppaux.rs:47:17
|
LL | fn foo<'z>() where &'z (): Sized {
| -------------------------------- function `foo` defined here
LL | / fn foo<'z>()
LL | | where
LL | | &'z (): Sized,
| |__________________- function `foo` defined here
...
LL | let x: () = foo::<'static>;
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
LL | let x: () = foo::<'static>;
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item
| |
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {foo::<'static>}`
Expand All @@ -71,18 +77,18 @@ LL | let x: () = foo::<'static>();
| ++

error[E0277]: the trait bound `str: Foo<'_, '_, u8>` is not satisfied
--> $DIR/substs-ppaux.rs:49:6
--> $DIR/substs-ppaux.rs:55:6
|
LL | <str as Foo<u8>>::bar;
| ^^^ the trait `Sized` is not implemented for `str`, which is required by `str: Foo<'_, '_, u8>`
|
note: required for `str` to implement `Foo<'_, '_, u8>`
--> $DIR/substs-ppaux.rs:11:17
--> $DIR/substs-ppaux.rs:15:20
|
LL | impl<'a,'b,T,S> Foo<'a, 'b, S> for T {}
| - ^^^^^^^^^^^^^^ ^
| |
| unsatisfied trait bound introduced here
LL | impl<'a, 'b, T, S> Foo<'a, 'b, S> for T {}
| - ^^^^^^^^^^^^^^ ^
| |
| unsatisfied trait bound introduced here

error: aborting due to 5 previous errors

Expand Down
30 changes: 18 additions & 12 deletions tests/ui/associated-types/substs-ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,51 @@
//
//@[verbose] compile-flags: -Z verbose-internals

trait Foo<'b, 'c, S=u32> {
fn bar<'a, T>() where T: 'a {}
trait Foo<'b, 'c, S = u32> {
fn bar<'a, T>()
where
T: 'a,
{
}
fn baz() {}
}

impl<'a,'b,T,S> Foo<'a, 'b, S> for T {}
impl<'a, 'b, T, S> Foo<'a, 'b, S> for T {}

fn main() {}

fn foo<'z>() where &'z (): Sized {
let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
fn foo<'z>()
where
&'z (): Sized,
{
let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>}`
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`


let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`

let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz}`
//[verbose]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`

let x: () = foo::<'static>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {foo::<ReStatic>}`
//[verbose]~| found fn item `fn() {foo::<'static>}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {foo::<'static>}`
Expand Down
Loading

0 comments on commit a385e56

Please sign in to comment.