Skip to content

Commit ce2f518

Browse files
Upgrade rustc crates and handle changes to canonicalization
They have to do with diagnostics, we could probably not support them but we will also someday want good diagnostics. The code is mostly copied from rustc.
1 parent 0cce47c commit ce2f518

File tree

12 files changed

+256
-116
lines changed

12 files changed

+256
-116
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,9 +1863,9 @@ dependencies = [
18631863

18641864
[[package]]
18651865
name = "ra-ap-rustc_abi"
1866-
version = "0.126.0"
1866+
version = "0.128.0"
18671867
source = "registry+https://github.com/rust-lang/crates.io-index"
1868-
checksum = "0c6789d94fb3e6e30d62f55e99a321ba63484a8bb3b4ead338687c9ddc282d28"
1868+
checksum = "8da95e732b424802b1f043ab4007c78a0fc515ab249587abbea4634bf5fdce9a"
18691869
dependencies = [
18701870
"bitflags 2.9.1",
18711871
"ra-ap-rustc_hashes",
@@ -1875,34 +1875,34 @@ dependencies = [
18751875

18761876
[[package]]
18771877
name = "ra-ap-rustc_ast_ir"
1878-
version = "0.126.0"
1878+
version = "0.128.0"
18791879
source = "registry+https://github.com/rust-lang/crates.io-index"
1880-
checksum = "aaab80bda0f05e9842e3afb7779b0bad0a4b54e0f7ba6deb5705dcf86482811d"
1880+
checksum = "3838d9d7a3a5cdc511cfb6ad78740ce532f75a2366d3fc3b9853ea1b5c872779"
18811881

18821882
[[package]]
18831883
name = "ra-ap-rustc_hashes"
1884-
version = "0.126.0"
1884+
version = "0.128.0"
18851885
source = "registry+https://github.com/rust-lang/crates.io-index"
1886-
checksum = "64bd405e538102b5f699241794b2eefee39d5414c0e4bc72435e91430c51f905"
1886+
checksum = "bdc8995d268d3bb4ece910f575ea5a063d6003e193ec155d15703b65882d53fb"
18871887
dependencies = [
18881888
"rustc-stable-hash",
18891889
]
18901890

18911891
[[package]]
18921892
name = "ra-ap-rustc_index"
1893-
version = "0.126.0"
1893+
version = "0.128.0"
18941894
source = "registry+https://github.com/rust-lang/crates.io-index"
1895-
checksum = "521621e271aa03b8433dad5981838278d6cfd7d2d8c9f4eb6d427f1d671f90fc"
1895+
checksum = "ed0ccdf6e5627c6c3e54e571e52ce0bc8b94d5f0b94b7460269ca68a4706be69"
18961896
dependencies = [
18971897
"ra-ap-rustc_index_macros",
18981898
"smallvec",
18991899
]
19001900

19011901
[[package]]
19021902
name = "ra-ap-rustc_index_macros"
1903-
version = "0.126.0"
1903+
version = "0.128.0"
19041904
source = "registry+https://github.com/rust-lang/crates.io-index"
1905-
checksum = "245e30f2e1fef258913cc548b36f575549c8af31cbc4649929d21deda96ceeb7"
1905+
checksum = "bd28f42362b5c9fb9b8766c3189df02a402b13363600c6885e11027889f03ee6"
19061906
dependencies = [
19071907
"proc-macro2",
19081908
"quote",
@@ -1911,9 +1911,9 @@ dependencies = [
19111911

19121912
[[package]]
19131913
name = "ra-ap-rustc_lexer"
1914-
version = "0.126.0"
1914+
version = "0.128.0"
19151915
source = "registry+https://github.com/rust-lang/crates.io-index"
1916-
checksum = "a82681f924500e888c860e60ed99e9bf702a219a69374f59116c4261525a2157"
1916+
checksum = "f1c31a82f091b910a27ee53a86a9af28a2df10c3484e2f1bbfe70633aa84dee9"
19171917
dependencies = [
19181918
"memchr",
19191919
"unicode-properties",
@@ -1922,9 +1922,9 @@ dependencies = [
19221922

19231923
[[package]]
19241924
name = "ra-ap-rustc_next_trait_solver"
1925-
version = "0.126.0"
1925+
version = "0.128.0"
19261926
source = "registry+https://github.com/rust-lang/crates.io-index"
1927-
checksum = "0c9ce51f2431fbdc7fabd2d957522b6e27f41f68ec2af74b52a6f4116352ce1a"
1927+
checksum = "f8cac6c2b5a8924209d4ca682cbc507252c58a664911e0ef463c112882ba6f72"
19281928
dependencies = [
19291929
"derive-where",
19301930
"ra-ap-rustc_index",
@@ -1935,19 +1935,19 @@ dependencies = [
19351935

19361936
[[package]]
19371937
name = "ra-ap-rustc_parse_format"
1938-
version = "0.126.0"
1938+
version = "0.128.0"
19391939
source = "registry+https://github.com/rust-lang/crates.io-index"
1940-
checksum = "adc85ef3fdb6c084bde84857d8948dc66b752129dc8417a8614ce490e99a143f"
1940+
checksum = "a085a1cf902dcca8abbc537faaef154bbccbbb51850f779ce5484ae3782b5d8f"
19411941
dependencies = [
19421942
"ra-ap-rustc_lexer",
19431943
"rustc-literal-escaper 0.0.5",
19441944
]
19451945

19461946
[[package]]
19471947
name = "ra-ap-rustc_pattern_analysis"
1948-
version = "0.126.0"
1948+
version = "0.128.0"
19491949
source = "registry+https://github.com/rust-lang/crates.io-index"
1950-
checksum = "3cd81eccf33d9528905d4e5abaa254b3129a6405d6c5f123fed9b73a3d217f35"
1950+
checksum = "8ba32e3985367bc34856b41c7604133649d4a367eb5d7bdf50623025731459d8"
19511951
dependencies = [
19521952
"ra-ap-rustc_index",
19531953
"rustc-hash 2.1.1",
@@ -1958,10 +1958,11 @@ dependencies = [
19581958

19591959
[[package]]
19601960
name = "ra-ap-rustc_type_ir"
1961-
version = "0.126.0"
1961+
version = "0.128.0"
19621962
source = "registry+https://github.com/rust-lang/crates.io-index"
1963-
checksum = "11cb0da02853698d9c89e1d1c01657b9969752befd56365e8899d4310e52b373"
1963+
checksum = "9c9911d72f75d85d21fe88374d7bcec94f2200feffb7234108a24cc3da7c3591"
19641964
dependencies = [
1965+
"arrayvec",
19651966
"bitflags 2.9.1",
19661967
"derive-where",
19671968
"ena",
@@ -1977,9 +1978,9 @@ dependencies = [
19771978

19781979
[[package]]
19791980
name = "ra-ap-rustc_type_ir_macros"
1980-
version = "0.126.0"
1981+
version = "0.128.0"
19811982
source = "registry+https://github.com/rust-lang/crates.io-index"
1982-
checksum = "ffc93adeb52c483ede13bee6680466458218243ab479c04fb71bb53925a6e0ff"
1983+
checksum = "22f539b87991683ce17cc52e62600fdf2b4a8af43952db30387edc1a576d3b43"
19831984
dependencies = [
19841985
"proc-macro2",
19851986
"quote",

src/tools/rust-analyzer/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
8989
vfs = { path = "./crates/vfs", version = "0.0.0" }
9090
edition = { path = "./crates/edition", version = "0.0.0" }
9191

92-
ra-ap-rustc_lexer = { version = "0.126", default-features = false }
93-
ra-ap-rustc_parse_format = { version = "0.126", default-features = false }
94-
ra-ap-rustc_index = { version = "0.126", default-features = false }
95-
ra-ap-rustc_abi = { version = "0.126", default-features = false }
96-
ra-ap-rustc_pattern_analysis = { version = "0.126", default-features = false }
97-
ra-ap-rustc_ast_ir = { version = "0.126", default-features = false }
98-
ra-ap-rustc_type_ir = { version = "0.126", default-features = false }
99-
ra-ap-rustc_next_trait_solver = { version = "0.126", default-features = false }
92+
ra-ap-rustc_lexer = { version = "0.128", default-features = false }
93+
ra-ap-rustc_parse_format = { version = "0.128", default-features = false }
94+
ra-ap-rustc_index = { version = "0.128", default-features = false }
95+
ra-ap-rustc_abi = { version = "0.128", default-features = false }
96+
ra-ap-rustc_pattern_analysis = { version = "0.128", default-features = false }
97+
ra-ap-rustc_ast_ir = { version = "0.128", default-features = false }
98+
ra-ap-rustc_type_ir = { version = "0.128", default-features = false }
99+
ra-ap-rustc_next_trait_solver = { version = "0.128", default-features = false }
100100

101101
# local crates that aren't published to crates.io. These should not have versions.
102102

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use rustc_index::Idx;
1010
use rustc_type_ir::InferTy::{self, FloatVar, IntVar, TyVar};
1111
use rustc_type_ir::inherent::{Const as _, IntoKind as _, Region as _, SliceLike, Ty as _};
1212
use rustc_type_ir::{
13-
BoundVar, CanonicalQueryInput, CanonicalTyVarKind, DebruijnIndex, Flags, InferConst,
14-
RegionKind, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
15-
UniverseIndex,
13+
BoundVar, CanonicalQueryInput, DebruijnIndex, Flags, InferConst, RegionKind, TyVid, TypeFlags,
14+
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, UniverseIndex,
1615
};
1716
use smallvec::SmallVec;
1817
use tracing::debug;
@@ -316,6 +315,13 @@ struct Canonicalizer<'cx, 'db> {
316315
// Note that indices is only used once `var_values` is big enough to be
317316
// heap-allocated.
318317
indices: FxHashMap<GenericArg<'db>, BoundVar>,
318+
/// Maps each `sub_unification_table_root_var` to the index of the first
319+
/// variable which used it.
320+
///
321+
/// This means in case two type variables have the same sub relations root,
322+
/// we set the `sub_root` of the second variable to the position of the first.
323+
/// Otherwise the `sub_root` of each type variable is just its own position.
324+
sub_root_lookup_table: FxHashMap<TyVid, usize>,
319325
canonicalize_mode: &'cx dyn CanonicalizeMode,
320326
needs_canonical_flags: TypeFlags,
321327

@@ -384,28 +390,27 @@ impl<'cx, 'db> TypeFolder<DbInterner<'db>> for Canonicalizer<'cx, 'db> {
384390
// FIXME: perf problem described in #55921.
385391
ui = UniverseIndex::ROOT;
386392
}
387-
self.canonicalize_ty_var(
388-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui)),
389-
t,
390-
)
393+
394+
let sub_root = self.get_or_insert_sub_root(vid);
395+
self.canonicalize_ty_var(CanonicalVarKind::Ty { ui, sub_root }, t)
391396
}
392397
}
393398
}
394399

395400
TyKind::Infer(IntVar(vid)) => {
396401
let nt = self.infcx.opportunistic_resolve_int_var(vid);
397402
if nt != t {
398-
self.fold_ty(nt)
403+
return self.fold_ty(nt);
399404
} else {
400-
self.canonicalize_ty_var(CanonicalVarKind::Ty(CanonicalTyVarKind::Int), t)
405+
self.canonicalize_ty_var(CanonicalVarKind::Int, t)
401406
}
402407
}
403408
TyKind::Infer(FloatVar(vid)) => {
404409
let nt = self.infcx.opportunistic_resolve_float_var(vid);
405410
if nt != t {
406-
self.fold_ty(nt)
411+
return self.fold_ty(nt);
407412
} else {
408-
self.canonicalize_ty_var(CanonicalVarKind::Ty(CanonicalTyVarKind::Float), t)
413+
self.canonicalize_ty_var(CanonicalVarKind::Float, t)
409414
}
410415
}
411416

@@ -579,6 +584,7 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> {
579584
variables: SmallVec::from_slice(base.variables.as_slice()),
580585
query_state,
581586
indices: FxHashMap::default(),
587+
sub_root_lookup_table: Default::default(),
582588
binder_index: DebruijnIndex::ZERO,
583589
};
584590
if canonicalizer.query_state.var_values.spilled() {
@@ -673,6 +679,13 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> {
673679
}
674680
}
675681

682+
fn get_or_insert_sub_root(&mut self, vid: TyVid) -> BoundVar {
683+
let root_vid = self.infcx.sub_unification_table_root_var(vid);
684+
let idx =
685+
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
686+
BoundVar::from(idx)
687+
}
688+
676689
/// Replaces the universe indexes used in `var_values` with their index in
677690
/// `query_state.universe_map`. This minimizes the maximum universe used in
678691
/// the canonicalized value.
@@ -692,9 +705,9 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> {
692705
self.variables
693706
.iter()
694707
.map(|v| match *v {
695-
CanonicalVarKind::Ty(CanonicalTyVarKind::Int | CanonicalTyVarKind::Float) => *v,
696-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(u)) => {
697-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(reverse_universe_map[&u]))
708+
CanonicalVarKind::Int | CanonicalVarKind::Float => *v,
709+
CanonicalVarKind::Ty { ui, sub_root } => {
710+
CanonicalVarKind::Ty { ui: reverse_universe_map[&ui], sub_root }
698711
}
699712
CanonicalVarKind::Region(u) => CanonicalVarKind::Region(reverse_universe_map[&u]),
700713
CanonicalVarKind::Const(u) => CanonicalVarKind::Const(reverse_universe_map[&u]),

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/infer/canonical/mod.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ use crate::next_solver::{
3232
};
3333
use instantiate::CanonicalExt;
3434
use rustc_index::IndexVec;
35+
use rustc_type_ir::inherent::IntoKind;
3536
use rustc_type_ir::{
36-
AliasRelationDirection, AliasTyKind, CanonicalTyVarKind, CanonicalVarKind, InferTy,
37-
TypeFoldable, UniverseIndex, Upcast, Variance,
37+
AliasRelationDirection, AliasTyKind, CanonicalVarKind, InferTy, TypeFoldable, UniverseIndex,
38+
Upcast, Variance,
3839
inherent::{SliceLike, Ty as _},
3940
relate::{
4041
Relate, TypeRelation, VarianceDiagInfo,
@@ -78,27 +79,15 @@ impl<'db> InferCtxt<'db> {
7879
.chain((1..=canonical.max_universe.as_u32()).map(|_| self.create_next_universe()))
7980
.collect();
8081

81-
let canonical_inference_vars =
82-
self.instantiate_canonical_vars(canonical.variables, |ui| universes[ui]);
83-
let result = canonical.instantiate(self.interner, &canonical_inference_vars);
84-
(result, canonical_inference_vars)
85-
}
86-
87-
/// Given the "infos" about the canonical variables from some
88-
/// canonical, creates fresh variables with the same
89-
/// characteristics (see `instantiate_canonical_var` for
90-
/// details). You can then use `instantiate` to instantiate the
91-
/// canonical variable with these inference variables.
92-
fn instantiate_canonical_vars(
93-
&self,
94-
variables: CanonicalVars<'db>,
95-
universe_map: impl Fn(UniverseIndex) -> UniverseIndex,
96-
) -> CanonicalVarValues<'db> {
97-
CanonicalVarValues {
98-
var_values: self.interner.mk_args_from_iter(
99-
variables.iter().map(|info| self.instantiate_canonical_var(info, &universe_map)),
100-
),
101-
}
82+
let var_values = CanonicalVarValues::instantiate(
83+
self.interner,
84+
canonical.variables,
85+
|var_values, info| {
86+
self.instantiate_canonical_var(info, &var_values, |ui| universes[ui])
87+
},
88+
);
89+
let result = canonical.instantiate(self.interner, &var_values);
90+
(result, var_values)
10291
}
10392

10493
/// Given the "info" about a canonical variable, creates a fresh
@@ -112,21 +101,27 @@ impl<'db> InferCtxt<'db> {
112101
pub fn instantiate_canonical_var(
113102
&self,
114103
cv_info: CanonicalVarKind<DbInterner<'db>>,
104+
previous_var_values: &[GenericArg<'db>],
115105
universe_map: impl Fn(UniverseIndex) -> UniverseIndex,
116106
) -> GenericArg<'db> {
117107
match cv_info {
118-
CanonicalVarKind::Ty(ty_kind) => {
119-
let ty = match ty_kind {
120-
CanonicalTyVarKind::General(ui) => {
121-
self.next_ty_var_in_universe(universe_map(ui))
108+
CanonicalVarKind::Ty { ui, sub_root } => {
109+
let vid = self.next_ty_var_id_in_universe(universe_map(ui));
110+
// If this inference variable is related to an earlier variable
111+
// via subtyping, we need to add that info to the inference context.
112+
if let Some(prev) = previous_var_values.get(sub_root.as_usize()) {
113+
if let TyKind::Infer(InferTy::TyVar(sub_root)) = prev.expect_ty().kind() {
114+
self.sub_unify_ty_vids_raw(vid, sub_root);
115+
} else {
116+
unreachable!()
122117
}
118+
}
119+
Ty::new_var(self.interner, vid).into()
120+
}
123121

124-
CanonicalTyVarKind::Int => self.next_int_var(),
122+
CanonicalVarKind::Int => self.next_int_var().into(),
125123

126-
CanonicalTyVarKind::Float => self.next_float_var(),
127-
};
128-
ty.into()
129-
}
124+
CanonicalVarKind::Float => self.next_float_var().into(),
130125

131126
CanonicalVarKind::PlaceholderTy(PlaceholderTy { universe, bound }) => {
132127
let universe_mapped = universe_map(universe);

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/infer/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,12 @@ impl<'db> rustc_type_ir::InferCtxtLike for InferCtxt<'db> {
313313
fn reset_opaque_types(&self) {
314314
let _ = self.take_opaque_types();
315315
}
316+
317+
fn sub_unification_table_root_var(&self, var: rustc_type_ir::TyVid) -> rustc_type_ir::TyVid {
318+
self.sub_unification_table_root_var(var)
319+
}
320+
321+
fn sub_unify_ty_vids_raw(&self, a: rustc_type_ir::TyVid, b: rustc_type_ir::TyVid) {
322+
self.sub_unify_ty_vids_raw(a, b);
323+
}
316324
}

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/infer/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,14 @@ impl<'db> InferCtxt<'db> {
10191019
}
10201020
}
10211021
}
1022+
1023+
fn sub_unification_table_root_var(&self, var: rustc_type_ir::TyVid) -> rustc_type_ir::TyVid {
1024+
self.inner.borrow_mut().type_variables().sub_unification_table_root_var(var)
1025+
}
1026+
1027+
fn sub_unify_ty_vids_raw(&self, a: rustc_type_ir::TyVid, b: rustc_type_ir::TyVid) {
1028+
self.inner.borrow_mut().type_variables().sub_unify(a, b);
1029+
}
10221030
}
10231031

10241032
/// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/infer/snapshot/undo_log.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Snapshot {
2525
pub(crate) enum UndoLog<'db> {
2626
DuplicateOpaqueType,
2727
OpaqueTypes(OpaqueTypeKey<'db>, Option<OpaqueHiddenType<'db>>),
28-
TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'db>>>),
28+
TypeVariables(type_variable::UndoLog<'db>),
2929
ConstUnificationTable(sv::UndoLog<ut::Delegate<ConstVidKey<'db>>>),
3030
IntUnificationTable(sv::UndoLog<ut::Delegate<IntVid>>),
3131
FloatUnificationTable(sv::UndoLog<ut::Delegate<FloatVid>>),
@@ -51,6 +51,8 @@ impl_from! {
5151
RegionConstraintCollector(region_constraints::UndoLog<'db>),
5252

5353
TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'db>>>),
54+
TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidSubKey>>),
55+
TypeVariables(type_variable::UndoLog<'db>),
5456
IntUnificationTable(sv::UndoLog<ut::Delegate<IntVid>>),
5557
FloatUnificationTable(sv::UndoLog<ut::Delegate<FloatVid>>),
5658

0 commit comments

Comments
 (0)