Skip to content

Commit e56bc53

Browse files
committed
Auto merge of rust-lang#125495 - lcnr:canonicalizer-bound-var-lookup, r=<try>
canonicalizer: add lookup table blocking this on being able to run perf, may be necessary for deeply nested types. r? `@compiler-errors`
2 parents 8fbdc04 + f572903 commit e56bc53

File tree

72 files changed

+455
-367
lines changed

Some content is hidden

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

72 files changed

+455
-367
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ fn test_unstable_options_tracking_hash() {
804804
tracked!(mir_opt_level, Some(4));
805805
tracked!(move_size_limit, Some(4096));
806806
tracked!(mutable_noalias, false);
807-
tracked!(next_solver, Some(NextSolverConfig { coherence: true, globally: false }));
807+
tracked!(next_solver, NextSolverConfig { coherence: true, globally: true });
808808
tracked!(no_generate_arange_section, true);
809809
tracked!(no_jump_tables, true);
810810
tracked!(no_link, true);

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3127,11 +3127,11 @@ impl<'tcx> TyCtxt<'tcx> {
31273127
}
31283128

31293129
pub fn next_trait_solver_globally(self) -> bool {
3130-
self.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.globally)
3130+
self.sess.opts.unstable_opts.next_solver.globally
31313131
}
31323132

31333133
pub fn next_trait_solver_in_coherence(self) -> bool {
3134-
self.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.coherence)
3134+
self.sess.opts.unstable_opts.next_solver.coherence
31353135
}
31363136

31373137
pub fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {

compiler/rustc_next_trait_solver/src/canonicalizer.rs

+49-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cmp::Ordering;
22

3+
use rustc_type_ir::data_structures::HashMap;
34
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
45
use rustc_type_ir::inherent::*;
56
use rustc_type_ir::visit::TypeVisitableExt;
@@ -44,6 +45,8 @@ pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
4445
canonicalize_mode: CanonicalizeMode,
4546

4647
variables: &'a mut Vec<I::GenericArg>,
48+
variable_lookup_table: HashMap<I::GenericArg, usize>,
49+
4750
primitive_var_infos: Vec<CanonicalVarInfo<I>>,
4851
binder_index: ty::DebruijnIndex,
4952
}
@@ -60,6 +63,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
6063
canonicalize_mode,
6164

6265
variables,
66+
variable_lookup_table: Default::default(),
6367
primitive_var_infos: Vec::new(),
6468
binder_index: ty::INNERMOST,
6569
};
@@ -75,6 +79,37 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
7579
Canonical { defining_opaque_types, max_universe, variables, value }
7680
}
7781

82+
fn get_or_insert_bound_var(
83+
&mut self,
84+
arg: impl Into<I::GenericArg>,
85+
canonical_var_info: CanonicalVarInfo<I>,
86+
) -> ty::BoundVar {
87+
// FIXME: 16 is made up and arbitrary. We should look at some
88+
// perf data here.
89+
let arg = arg.into();
90+
let idx = if self.variables.len() > 16 {
91+
if self.variable_lookup_table.is_empty() {
92+
self.variable_lookup_table.extend(self.variables.iter().copied().zip(0..));
93+
}
94+
95+
*self.variable_lookup_table.entry(arg).or_insert_with(|| {
96+
let var = self.variables.len();
97+
self.variables.push(arg);
98+
self.primitive_var_infos.push(canonical_var_info);
99+
var
100+
})
101+
} else {
102+
self.variables.iter().position(|&v| v == arg).unwrap_or_else(|| {
103+
let var = self.variables.len();
104+
self.variables.push(arg);
105+
self.primitive_var_infos.push(canonical_var_info);
106+
var
107+
})
108+
};
109+
110+
ty::BoundVar::from(idx)
111+
}
112+
78113
fn finalize(self) -> (ty::UniverseIndex, I::CanonicalVars) {
79114
let mut var_infos = self.primitive_var_infos;
80115
// See the rustc-dev-guide section about how we deal with universes
@@ -124,8 +159,8 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
124159
// - var_infos: [E0, U1, E2, U1, E1, E6, U6], curr_compressed_uv: 2, next_orig_uv: 6
125160
// - var_infos: [E0, U1, E1, U1, E1, E3, U3], curr_compressed_uv: 2, next_orig_uv: -
126161
//
127-
// This algorithm runs in `O()` where `n` is the number of different universe
128-
// indices in the input. This should be fine as `n` is expected to be small.
162+
// This algorithm runs in `O(mn)` where `n` is the number of different universes and
163+
// `m` the number of variables. This should be fine as both are expected to be small.
129164
let mut curr_compressed_uv = ty::UniverseIndex::ROOT;
130165
let mut existential_in_new_uv = None;
131166
let mut next_orig_uv = Some(ty::UniverseIndex::ROOT);
@@ -279,20 +314,20 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
279314
}
280315
};
281316

282-
let existing_bound_var = match self.canonicalize_mode {
283-
CanonicalizeMode::Input => None,
317+
let var = match self.canonicalize_mode {
318+
CanonicalizeMode::Input => {
319+
// It's fine to not add `r` to the lookup table, as we will
320+
// never lookup regions when canonicalizing inputs.
321+
let var = ty::BoundVar::from(self.variables.len());
322+
self.variables.push(r.into());
323+
self.primitive_var_infos.push(CanonicalVarInfo { kind });
324+
var
325+
}
284326
CanonicalizeMode::Response { .. } => {
285-
self.variables.iter().position(|&v| v == r.into()).map(ty::BoundVar::from)
327+
self.get_or_insert_bound_var(r, CanonicalVarInfo { kind })
286328
}
287329
};
288330

289-
let var = existing_bound_var.unwrap_or_else(|| {
290-
let var = ty::BoundVar::from(self.variables.len());
291-
self.variables.push(r.into());
292-
self.primitive_var_infos.push(CanonicalVarInfo { kind });
293-
var
294-
});
295-
296331
Region::new_anon_bound(self.cx(), self.binder_index, var)
297332
}
298333

@@ -373,14 +408,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
373408
| ty::Error(_) => return t.super_fold_with(self),
374409
};
375410

376-
let var = ty::BoundVar::from(
377-
self.variables.iter().position(|&v| v == t.into()).unwrap_or_else(|| {
378-
let var = self.variables.len();
379-
self.variables.push(t.into());
380-
self.primitive_var_infos.push(CanonicalVarInfo { kind });
381-
var
382-
}),
383-
);
411+
let var = self.get_or_insert_bound_var(t, CanonicalVarInfo { kind });
384412

385413
Ty::new_anon_bound(self.cx(), self.binder_index, var)
386414
}
@@ -421,14 +449,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
421449
| ty::ConstKind::Expr(_) => return c.super_fold_with(self),
422450
};
423451

424-
let var = ty::BoundVar::from(
425-
self.variables.iter().position(|&v| v == c.into()).unwrap_or_else(|| {
426-
let var = self.variables.len();
427-
self.variables.push(c.into());
428-
self.primitive_var_infos.push(CanonicalVarInfo { kind });
429-
var
430-
}),
431-
);
452+
let var = self.get_or_insert_bound_var(c, CanonicalVarInfo { kind });
432453

433454
Const::new_anon_bound(self.cx(), self.binder_index, var)
434455
}

compiler/rustc_session/src/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,11 @@ pub struct NextSolverConfig {
823823
/// This is only `true` if `coherence` is also enabled.
824824
pub globally: bool,
825825
}
826+
impl Default for NextSolverConfig {
827+
fn default() -> Self {
828+
NextSolverConfig { coherence: true, globally: false }
829+
}
830+
}
826831

827832
#[derive(Clone)]
828833
pub enum Input {

compiler/rustc_session/src/options.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ mod desc {
402402
pub const parse_unpretty: &str = "`string` or `string=string`";
403403
pub const parse_treat_err_as_bug: &str = "either no value or a non-negative number";
404404
pub const parse_next_solver_config: &str =
405-
"a comma separated list of solver configurations: `globally` (default), and `coherence`";
405+
"either `globally` (when used without an argument), `coherence` (default) or `no`";
406406
pub const parse_lto: &str =
407407
"either a boolean (`yes`, `no`, `on`, `off`, etc), `thin`, `fat`, or omitted";
408408
pub const parse_linker_plugin_lto: &str =
@@ -1088,27 +1088,16 @@ mod parse {
10881088
}
10891089
}
10901090

1091-
pub(crate) fn parse_next_solver_config(
1092-
slot: &mut Option<NextSolverConfig>,
1093-
v: Option<&str>,
1094-
) -> bool {
1091+
pub(crate) fn parse_next_solver_config(slot: &mut NextSolverConfig, v: Option<&str>) -> bool {
10951092
if let Some(config) = v {
1096-
let mut coherence = false;
1097-
let mut globally = true;
1098-
for c in config.split(',') {
1099-
match c {
1100-
"globally" => globally = true,
1101-
"coherence" => {
1102-
globally = false;
1103-
coherence = true;
1104-
}
1105-
_ => return false,
1106-
}
1107-
}
1108-
1109-
*slot = Some(NextSolverConfig { coherence: coherence || globally, globally });
1093+
*slot = match config {
1094+
"no" => NextSolverConfig { coherence: false, globally: false },
1095+
"coherence" => NextSolverConfig { coherence: true, globally: false },
1096+
"globally" => NextSolverConfig { coherence: true, globally: true },
1097+
_ => return false,
1098+
};
11101099
} else {
1111-
*slot = Some(NextSolverConfig { coherence: true, globally: true });
1100+
*slot = NextSolverConfig { coherence: true, globally: true };
11121101
}
11131102

11141103
true
@@ -1842,7 +1831,7 @@ options! {
18421831
"the size at which the `large_assignments` lint starts to be emitted"),
18431832
mutable_noalias: bool = (true, parse_bool, [TRACKED],
18441833
"emit noalias metadata for mutable references (default: yes)"),
1845-
next_solver: Option<NextSolverConfig> = (None, parse_next_solver_config, [TRACKED],
1834+
next_solver: NextSolverConfig = (NextSolverConfig::default(), parse_next_solver_config, [TRACKED],
18461835
"enable and configure the next generation trait solver used by rustc"),
18471836
nll_facts: bool = (false, parse_bool, [UNTRACKED],
18481837
"dump facts from NLL analysis into side files (default: no)"),

compiler/rustc_trait_selection/src/traits/engine.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ where
3535
if infcx.next_trait_solver() {
3636
Box::new(NextFulfillmentCtxt::new(infcx))
3737
} else {
38-
let new_solver_globally =
39-
infcx.tcx.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.globally);
4038
assert!(
41-
!new_solver_globally,
39+
!infcx.tcx.next_trait_solver_globally(),
4240
"using old solver even though new solver is enabled globally"
4341
);
4442
Box::new(FulfillmentContext::new(infcx))

tests/crashes/118987.rs

-17
This file was deleted.

tests/ui/associated-types/associated-types-coherence-failure.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `Cow<'_, _>`
1+
error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `<_ as ToOwned>::Owned`
22
--> $DIR/associated-types-coherence-failure.rs:21:1
33
|
44
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for <B as ToOwned>::Owned where B: ToOwned {
55
| ----------------------------------------------------------------------------- first implementation here
66
...
77
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Cow<'_, _>`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `<_ as ToOwned>::Owned`
99

10-
error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `&_`
10+
error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `<_ as ToOwned>::Owned`
1111
--> $DIR/associated-types-coherence-failure.rs:28:1
1212
|
1313
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for <B as ToOwned>::Owned where B: ToOwned {
1414
| ----------------------------------------------------------------------------- first implementation here
1515
...
1616
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for &'a B where B: ToOwned {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `<_ as ToOwned>::Owned`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/auto-traits/opaque_type_candidate_selection.rs

-30
This file was deleted.

tests/ui/coherence/coherence-negative-outlives-lifetimes.stock.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
55
| ---------------------------------------------- first implementation here
66
LL | impl<'a, T> MyTrait<'a> for &'a T {}
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
8+
|
9+
= note: downstream crates may implement trait `MyPredicate<'_>` for type `&_`
810

911
error: aborting due to 1 previous error
1012

tests/ui/coherence/coherence-negative-outlives-lifetimes.with_negative_coherence.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | impl<'a, T: MyPredicate<'a>> MyTrait<'a> for T {}
55
| ---------------------------------------------- first implementation here
66
LL | impl<'a, T> MyTrait<'a> for &'a T {}
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
8+
|
9+
= note: downstream crates may implement trait `MyPredicate<'_>` for type `&_`
810

911
error: aborting due to 1 previous error
1012

tests/ui/coherence/coherence-overlap-negate-not-use-feature-gate.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | impl<T: DerefMut> Foo for T {}
55
| --------------------------- first implementation here
66
LL | impl<U> Foo for &U {}
77
| ^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
8+
|
9+
= note: downstream crates may implement trait `std::ops::DerefMut` for type `&_`
810

911
error: aborting due to 1 previous error
1012

tests/ui/coherence/coherence-overlap-unnormalizable-projection-0.classic.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | impl<T> Trait for Box<T> {}
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Box<_>`
1313
|
1414
= note: downstream crates may implement trait `WithAssoc<'a>` for type `std::boxed::Box<_>`
15-
= note: downstream crates may implement trait `WhereBound` for type `<std::boxed::Box<_> as WithAssoc<'a>>::Assoc`
1615

1716
error: aborting due to 1 previous error
1817

tests/ui/coherence/coherence-overlap-unnormalizable-projection-1.classic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | impl<T> Trait for Box<T> {}
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Box<_>`
1313
|
1414
= note: downstream crates may implement trait `WithAssoc<'a>` for type `std::boxed::Box<_>`
15-
= note: downstream crates may implement trait `WhereBound` for type `std::boxed::Box<<std::boxed::Box<_> as WithAssoc<'a>>::Assoc>`
15+
= note: downstream crates may implement trait `WhereBound` for type `std::boxed::Box<_>`
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/coherence/negative-coherence-check-placeholder-outlives.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | impl<T> Bar for T where T: Foo {}
55
| ------------------------------ first implementation here
66
LL | impl<T> Bar for Box<T> {}
77
| ^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Box<_>`
8+
|
9+
= note: downstream crates may implement trait `Foo` for type `std::boxed::Box<_>`
810

911
error: aborting due to 1 previous error
1012

tests/ui/coherence/negative-coherence-considering-regions.any_lt.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | impl<T> Bar for T where T: Foo {}
66
...
77
LL | impl<T> Bar for &T {}
88
| ^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
9+
|
10+
= note: downstream crates may implement trait `Foo` for type `&_`
911

1012
error: aborting due to 1 previous error
1113

tests/ui/coherence/negative-coherence-placeholder-region-constraints-on-unification.explicit.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | impl<T: ?Sized> FnMarker for fn(&T) {}
88
|
99
= warning: the behavior may change in a future release
1010
= note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>
11+
= note: downstream crates may implement trait `Marker` for type `&_`
1112
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
1213
note: the lint level is defined here
1314
--> $DIR/negative-coherence-placeholder-region-constraints-on-unification.rs:4:11

tests/ui/coherence/normalize-for-errors.current.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
error[E0119]: conflicting implementations of trait `MyTrait<_>` for type `(Box<(MyType,)>, _)`
1+
error[E0119]: conflicting implementations of trait `MyTrait<_>` for type `(Box<(MyType,)>, <_ as Iterator>::Item)`
22
--> $DIR/normalize-for-errors.rs:17:1
33
|
44
LL | impl<T: Copy, S: Iterator> MyTrait<S> for (T, S::Item) {}
55
| ------------------------------------------------------ first implementation here
66
LL |
77
LL | impl<S: Iterator> MyTrait<S> for (Box<<(MyType,) as Mirror>::Assoc>, S::Item) {}
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(Box<(MyType,)>, _)`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(Box<(MyType,)>, <_ as Iterator>::Item)`
99
|
10+
= note: upstream crates may add a new impl of trait `std::clone::Clone` for type `(MyType,)` in future versions
1011
= note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::boxed::Box<(MyType,)>` in future versions
1112

1213
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)