Skip to content

Commit dc3a353

Browse files
committed
Auto merge of #118763 - matthiaskrgr:rollup-mgyf5hp, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #117586 (Uplift the (new solver) canonicalizer into `rustc_next_trait_solver`) - #118502 (fix: correct the arg for 'suggest to use associated function syntax' diagnostic) - #118694 (Add instance evaluation and methods to read an allocation in StableMIR) - #118715 (privacy: visit trait def id of projections) - #118730 (recurse into refs when comparing tys for diagnostics) - #118736 (temporarily revert "ice on ambguity in mir typeck") r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2d2f1b2 + a255b52 commit dc3a353

File tree

103 files changed

+1354
-538
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1354
-538
lines changed

Cargo.lock

+9
Original file line numberDiff line numberDiff line change
@@ -4303,6 +4303,13 @@ dependencies = [
43034303
"tracing",
43044304
]
43054305

4306+
[[package]]
4307+
name = "rustc_next_trait_solver"
4308+
version = "0.0.0"
4309+
dependencies = [
4310+
"rustc_type_ir",
4311+
]
4312+
43064313
[[package]]
43074314
name = "rustc_parse"
43084315
version = "0.0.0"
@@ -4487,6 +4494,7 @@ dependencies = [
44874494
name = "rustc_smir"
44884495
version = "0.0.0"
44894496
dependencies = [
4497+
"rustc_abi",
44904498
"rustc_data_structures",
44914499
"rustc_hir",
44924500
"rustc_middle",
@@ -4571,6 +4579,7 @@ dependencies = [
45714579
"rustc_infer",
45724580
"rustc_macros",
45734581
"rustc_middle",
4582+
"rustc_next_trait_solver",
45744583
"rustc_parse_format",
45754584
"rustc_query_system",
45764585
"rustc_session",

compiler/rustc_hir_typeck/src/method/suggest.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1591,10 +1591,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15911591
{
15921592
let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
15931593
sig.inputs().skip_binder().get(0).and_then(|first| {
1594-
if first.peel_refs() == rcvr_ty.peel_refs() {
1595-
None
1596-
} else {
1594+
let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
1595+
// if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
1596+
if first.peel_refs() == impl_ty {
15971597
Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
1598+
} else {
1599+
None
15981600
}
15991601
})
16001602
} else {

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

+52-42
Original file line numberDiff line numberDiff line change
@@ -1181,37 +1181,54 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
11811181
debug!("cmp(t1={}, t1.kind={:?}, t2={}, t2.kind={:?})", t1, t1.kind(), t2, t2.kind());
11821182

11831183
// helper functions
1184-
fn equals<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
1185-
match (a.kind(), b.kind()) {
1186-
(a, b) if *a == *b => true,
1187-
(&ty::Int(_), &ty::Infer(ty::InferTy::IntVar(_)))
1188-
| (
1189-
&ty::Infer(ty::InferTy::IntVar(_)),
1190-
&ty::Int(_) | &ty::Infer(ty::InferTy::IntVar(_)),
1191-
)
1192-
| (&ty::Float(_), &ty::Infer(ty::InferTy::FloatVar(_)))
1193-
| (
1194-
&ty::Infer(ty::InferTy::FloatVar(_)),
1195-
&ty::Float(_) | &ty::Infer(ty::InferTy::FloatVar(_)),
1196-
) => true,
1197-
_ => false,
1184+
let recurse = |t1, t2, values: &mut (DiagnosticStyledString, DiagnosticStyledString)| {
1185+
let (x1, x2) = self.cmp(t1, t2);
1186+
(values.0).0.extend(x1.0);
1187+
(values.1).0.extend(x2.0);
1188+
};
1189+
1190+
fn fmt_region<'tcx>(region: ty::Region<'tcx>) -> String {
1191+
let mut r = region.to_string();
1192+
if r == "'_" {
1193+
r.clear();
1194+
} else {
1195+
r.push(' ');
11981196
}
1197+
format!("&{r}")
11991198
}
12001199

1201-
fn push_ty_ref<'tcx>(
1200+
fn push_ref<'tcx>(
12021201
region: ty::Region<'tcx>,
1203-
ty: Ty<'tcx>,
12041202
mutbl: hir::Mutability,
12051203
s: &mut DiagnosticStyledString,
12061204
) {
1207-
let mut r = region.to_string();
1208-
if r == "'_" {
1209-
r.clear();
1205+
s.push_highlighted(fmt_region(region));
1206+
s.push_highlighted(mutbl.prefix_str());
1207+
}
1208+
1209+
fn cmp_ty_refs<'tcx>(
1210+
r1: ty::Region<'tcx>,
1211+
mut1: hir::Mutability,
1212+
r2: ty::Region<'tcx>,
1213+
mut2: hir::Mutability,
1214+
ss: &mut (DiagnosticStyledString, DiagnosticStyledString),
1215+
) {
1216+
let (r1, r2) = (fmt_region(r1), fmt_region(r2));
1217+
if r1 != r2 {
1218+
ss.0.push_highlighted(r1);
1219+
ss.1.push_highlighted(r2);
12101220
} else {
1211-
r.push(' ');
1221+
ss.0.push_normal(r1);
1222+
ss.1.push_normal(r2);
1223+
}
1224+
1225+
if mut1 != mut2 {
1226+
ss.0.push_highlighted(mut1.prefix_str());
1227+
ss.1.push_highlighted(mut2.prefix_str());
1228+
} else {
1229+
ss.0.push_normal(mut1.prefix_str());
1230+
ss.1.push_normal(mut2.prefix_str());
12121231
}
1213-
s.push_highlighted(format!("&{}{}", r, mutbl.prefix_str()));
1214-
s.push_normal(ty.to_string());
12151232
}
12161233

12171234
// process starts here
@@ -1310,9 +1327,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
13101327
values.0.push_normal("_");
13111328
values.1.push_normal("_");
13121329
} else {
1313-
let (x1, x2) = self.cmp(ta1, ta2);
1314-
(values.0).0.extend(x1.0);
1315-
(values.1).0.extend(x2.0);
1330+
recurse(ta1, ta2, &mut values);
13161331
}
13171332
self.push_comma(&mut values.0, &mut values.1, len, i);
13181333
}
@@ -1418,27 +1433,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
14181433
}
14191434
}
14201435

1421-
// When finding T != &T, highlight only the borrow
1422-
(&ty::Ref(r1, ref_ty1, mutbl1), _) if equals(ref_ty1, t2) => {
1436+
// When finding `&T != &T`, compare the references, then recurse into pointee type
1437+
(&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2)) => {
14231438
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
1424-
push_ty_ref(r1, ref_ty1, mutbl1, &mut values.0);
1425-
values.1.push_normal(t2.to_string());
1439+
cmp_ty_refs(r1, mutbl1, r2, mutbl2, &mut values);
1440+
recurse(ref_ty1, ref_ty2, &mut values);
14261441
values
14271442
}
1428-
(_, &ty::Ref(r2, ref_ty2, mutbl2)) if equals(t1, ref_ty2) => {
1443+
// When finding T != &T, highlight the borrow
1444+
(&ty::Ref(r1, ref_ty1, mutbl1), _) => {
14291445
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
1430-
values.0.push_normal(t1.to_string());
1431-
push_ty_ref(r2, ref_ty2, mutbl2, &mut values.1);
1446+
push_ref(r1, mutbl1, &mut values.0);
1447+
recurse(ref_ty1, t2, &mut values);
14321448
values
14331449
}
1434-
1435-
// When encountering &T != &mut T, highlight only the borrow
1436-
(&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2))
1437-
if equals(ref_ty1, ref_ty2) =>
1438-
{
1450+
(_, &ty::Ref(r2, ref_ty2, mutbl2)) => {
14391451
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
1440-
push_ty_ref(r1, ref_ty1, mutbl1, &mut values.0);
1441-
push_ty_ref(r2, ref_ty2, mutbl2, &mut values.1);
1452+
push_ref(r2, mutbl2, &mut values.1);
1453+
recurse(t1, ref_ty2, &mut values);
14421454
values
14431455
}
14441456

@@ -1448,9 +1460,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
14481460
(DiagnosticStyledString::normal("("), DiagnosticStyledString::normal("("));
14491461
let len = args1.len();
14501462
for (i, (left, right)) in args1.iter().zip(args2).enumerate() {
1451-
let (x1, x2) = self.cmp(left, right);
1452-
(values.0).0.extend(x1.0);
1453-
(values.1).0.extend(x2.0);
1463+
recurse(left, right, &mut values);
14541464
self.push_comma(&mut values.0, &mut values.1, len, i);
14551465
}
14561466
if len == 1 {

compiler/rustc_infer/src/infer/mod.rs

+50-22
Original file line numberDiff line numberDiff line change
@@ -345,37 +345,61 @@ pub struct InferCtxt<'tcx> {
345345
impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
346346
type Interner = TyCtxt<'tcx>;
347347

348-
fn universe_of_ty(&self, ty: ty::InferTy) -> Option<ty::UniverseIndex> {
349-
use InferTy::*;
350-
match ty {
351-
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
352-
// ty infers will give you the universe of the var it resolved to not the universe
353-
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
354-
// try to print out `?0.1` it will just print `?0`.
355-
TyVar(ty_vid) => match self.probe_ty_var(ty_vid) {
356-
Err(universe) => Some(universe),
357-
Ok(_) => None,
358-
},
359-
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => None,
348+
fn interner(&self) -> TyCtxt<'tcx> {
349+
self.tcx
350+
}
351+
352+
fn universe_of_ty(&self, vid: TyVid) -> Option<ty::UniverseIndex> {
353+
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
354+
// ty infers will give you the universe of the var it resolved to not the universe
355+
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
356+
// try to print out `?0.1` it will just print `?0`.
357+
match self.probe_ty_var(vid) {
358+
Err(universe) => Some(universe),
359+
Ok(_) => None,
360360
}
361361
}
362362

363-
fn universe_of_ct(&self, ct: ty::InferConst) -> Option<ty::UniverseIndex> {
364-
use ty::InferConst::*;
365-
match ct {
366-
// Same issue as with `universe_of_ty`
367-
Var(ct_vid) => match self.probe_const_var(ct_vid) {
368-
Err(universe) => Some(universe),
369-
Ok(_) => None,
370-
},
371-
EffectVar(_) => None,
372-
Fresh(_) => None,
363+
fn universe_of_ct(&self, ct: ConstVid) -> Option<ty::UniverseIndex> {
364+
// Same issue as with `universe_of_ty`
365+
match self.probe_const_var(ct) {
366+
Err(universe) => Some(universe),
367+
Ok(_) => None,
373368
}
374369
}
375370

376371
fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
377372
Some(self.universe_of_region_vid(lt))
378373
}
374+
375+
fn root_ty_var(&self, vid: TyVid) -> TyVid {
376+
self.root_var(vid)
377+
}
378+
379+
fn probe_ty_var(&self, vid: TyVid) -> Option<Ty<'tcx>> {
380+
self.probe_ty_var(vid).ok()
381+
}
382+
383+
fn root_lt_var(&self, vid: ty::RegionVid) -> ty::RegionVid {
384+
self.root_region_var(vid)
385+
}
386+
387+
fn probe_lt_var(&self, vid: ty::RegionVid) -> Option<ty::Region<'tcx>> {
388+
let re = self
389+
.inner
390+
.borrow_mut()
391+
.unwrap_region_constraints()
392+
.opportunistic_resolve_var(self.tcx, vid);
393+
if re.is_var() { None } else { Some(re) }
394+
}
395+
396+
fn root_ct_var(&self, vid: ConstVid) -> ConstVid {
397+
self.root_const_var(vid)
398+
}
399+
400+
fn probe_ct_var(&self, vid: ConstVid) -> Option<ty::Const<'tcx>> {
401+
self.probe_const_var(vid).ok()
402+
}
379403
}
380404

381405
/// See the `error_reporting` module for more details.
@@ -1347,6 +1371,10 @@ impl<'tcx> InferCtxt<'tcx> {
13471371
self.inner.borrow_mut().type_variables().root_var(var)
13481372
}
13491373

1374+
pub fn root_region_var(&self, var: ty::RegionVid) -> ty::RegionVid {
1375+
self.inner.borrow_mut().unwrap_region_constraints().root_var(var)
1376+
}
1377+
13501378
pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
13511379
self.inner.borrow_mut().const_unification_table().find(var).vid
13521380
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
623623
}
624624
}
625625

626+
pub fn root_var(&mut self, vid: ty::RegionVid) -> ty::RegionVid {
627+
let mut ut = self.unification_table_mut(); // FIXME(rust-lang/ena#42): unnecessary mut
628+
ut.find(vid).vid
629+
}
630+
626631
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {
627632
match t {
628633
Glb => &mut self.glbs,

compiler/rustc_middle/src/ty/consts.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_macros::HashStable;
10-
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
10+
use rustc_type_ir::{ConstTy, IntoKind, TypeFlags, WithCachedTypeInfo};
1111

1212
mod int;
1313
mod kind;
@@ -26,6 +26,20 @@ use super::sty::ConstKind;
2626
#[rustc_pass_by_value]
2727
pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstData<'tcx>>>);
2828

29+
impl<'tcx> IntoKind for Const<'tcx> {
30+
type Kind = ConstKind<'tcx>;
31+
32+
fn kind(self) -> ConstKind<'tcx> {
33+
self.kind().clone()
34+
}
35+
}
36+
37+
impl<'tcx> ConstTy<TyCtxt<'tcx>> for Const<'tcx> {
38+
fn ty(self) -> Ty<'tcx> {
39+
self.ty()
40+
}
41+
}
42+
2943
/// Typed constant value.
3044
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)]
3145
pub struct ConstData<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+25
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
133133
) -> (Self::Ty, ty::Mutability) {
134134
(ty, mutbl)
135135
}
136+
137+
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
138+
self.mk_canonical_var_infos(infos)
139+
}
140+
141+
fn mk_bound_ty(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Ty {
142+
Ty::new_bound(self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
143+
}
144+
145+
fn mk_bound_region(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Region {
146+
Region::new_bound(
147+
self,
148+
debruijn,
149+
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
150+
)
151+
}
152+
153+
fn mk_bound_const(
154+
self,
155+
debruijn: ty::DebruijnIndex,
156+
var: ty::BoundVar,
157+
ty: Self::Ty,
158+
) -> Self::Const {
159+
Const::new_bound(self, debruijn, var, ty)
160+
}
136161
}
137162

138163
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;

0 commit comments

Comments
 (0)