Skip to content

Commit 8de8f46

Browse files
committed
Swap PredicateObligation to ThinVec
1 parent 7ec06b0 commit 8de8f46

File tree

12 files changed

+73
-42
lines changed

12 files changed

+73
-42
lines changed

Cargo.lock

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is automatically @generated by Cargo.
22
# It is not intended for manual editing.
3-
version = 3
3+
version = 4
44

55
[[package]]
66
name = "addr2line"
@@ -3857,6 +3857,7 @@ dependencies = [
38573857
"rustc_target",
38583858
"rustc_type_ir",
38593859
"smallvec",
3860+
"thin-vec",
38603861
"tracing",
38613862
]
38623863

@@ -4490,6 +4491,7 @@ dependencies = [
44904491
"rustc_transmute",
44914492
"rustc_type_ir",
44924493
"smallvec",
4494+
"thin-vec",
44934495
"tracing",
44944496
]
44954497

@@ -4561,6 +4563,7 @@ dependencies = [
45614563
"rustc_span",
45624564
"rustc_type_ir_macros",
45634565
"smallvec",
4566+
"thin-vec",
45644567
"tracing",
45654568
]
45664569

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use std::fmt::Debug;
7575
use std::hash;
7676
use std::marker::PhantomData;
7777

78+
use thin_vec::ThinVec;
7879
use tracing::debug;
7980

8081
use crate::fx::{FxHashMap, FxHashSet};
@@ -141,7 +142,7 @@ pub trait ObligationProcessor {
141142
#[derive(Debug)]
142143
pub enum ProcessResult<O, E> {
143144
Unchanged,
144-
Changed(Vec<O>),
145+
Changed(ThinVec<O>),
145146
Error(E),
146147
}
147148

compiler/rustc_data_structures/src/obligation_forest/tests.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt;
22

3+
use thin_vec::thin_vec;
4+
35
use super::*;
46

57
impl<'a> super::ForestObligation for &'a str {
@@ -101,9 +103,9 @@ fn push_pop() {
101103
// |-> A.3
102104
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
103105
|obligation| match *obligation {
104-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
106+
"A" => ProcessResult::Changed(thin_vec!["A.1", "A.2", "A.3"]),
105107
"B" => ProcessResult::Error("B is for broken"),
106-
"C" => ProcessResult::Changed(vec![]),
108+
"C" => ProcessResult::Changed(thin_vec![]),
107109
"A.1" | "A.2" | "A.3" => ProcessResult::Unchanged,
108110
_ => unreachable!(),
109111
},
@@ -123,8 +125,8 @@ fn push_pop() {
123125
|obligation| match *obligation {
124126
"A.1" => ProcessResult::Unchanged,
125127
"A.2" => ProcessResult::Unchanged,
126-
"A.3" => ProcessResult::Changed(vec!["A.3.i"]),
127-
"D" => ProcessResult::Changed(vec!["D.1", "D.2"]),
128+
"A.3" => ProcessResult::Changed(thin_vec!["A.3.i"]),
129+
"D" => ProcessResult::Changed(thin_vec!["D.1", "D.2"]),
128130
"A.3.i" | "D.1" | "D.2" => ProcessResult::Unchanged,
129131
_ => unreachable!(),
130132
},
@@ -139,11 +141,11 @@ fn push_pop() {
139141
// |-> D.2 |-> D.2.i
140142
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
141143
|obligation| match *obligation {
142-
"A.1" => ProcessResult::Changed(vec![]),
144+
"A.1" => ProcessResult::Changed(thin_vec![]),
143145
"A.2" => ProcessResult::Error("A is for apple"),
144-
"A.3.i" => ProcessResult::Changed(vec![]),
145-
"D.1" => ProcessResult::Changed(vec!["D.1.i"]),
146-
"D.2" => ProcessResult::Changed(vec!["D.2.i"]),
146+
"A.3.i" => ProcessResult::Changed(thin_vec![]),
147+
"D.1" => ProcessResult::Changed(thin_vec!["D.1.i"]),
148+
"D.2" => ProcessResult::Changed(thin_vec!["D.2.i"]),
147149
"D.1.i" | "D.2.i" => ProcessResult::Unchanged,
148150
_ => unreachable!(),
149151
},
@@ -158,7 +160,7 @@ fn push_pop() {
158160
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
159161
|obligation| match *obligation {
160162
"D.1.i" => ProcessResult::Error("D is for dumb"),
161-
"D.2.i" => ProcessResult::Changed(vec![]),
163+
"D.2.i" => ProcessResult::Changed(thin_vec![]),
162164
_ => panic!("unexpected obligation {:?}", obligation),
163165
},
164166
|_| {},
@@ -184,10 +186,10 @@ fn success_in_grandchildren() {
184186

185187
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
186188
|obligation| match *obligation {
187-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
188-
"A.1" => ProcessResult::Changed(vec![]),
189-
"A.2" => ProcessResult::Changed(vec!["A.2.i", "A.2.ii"]),
190-
"A.3" => ProcessResult::Changed(vec![]),
189+
"A" => ProcessResult::Changed(thin_vec!["A.1", "A.2", "A.3"]),
190+
"A.1" => ProcessResult::Changed(thin_vec![]),
191+
"A.2" => ProcessResult::Changed(thin_vec!["A.2.i", "A.2.ii"]),
192+
"A.3" => ProcessResult::Changed(thin_vec![]),
191193
"A.2.i" | "A.2.ii" => ProcessResult::Unchanged,
192194
_ => unreachable!(),
193195
},
@@ -201,7 +203,7 @@ fn success_in_grandchildren() {
201203
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
202204
|obligation| match *obligation {
203205
"A.2.i" => ProcessResult::Unchanged,
204-
"A.2.ii" => ProcessResult::Changed(vec![]),
206+
"A.2.ii" => ProcessResult::Changed(thin_vec![]),
205207
_ => unreachable!(),
206208
},
207209
|_| {},
@@ -211,7 +213,7 @@ fn success_in_grandchildren() {
211213

212214
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
213215
|obligation| match *obligation {
214-
"A.2.i" => ProcessResult::Changed(vec!["A.2.i.a"]),
216+
"A.2.i" => ProcessResult::Changed(thin_vec!["A.2.i.a"]),
215217
"A.2.i.a" => ProcessResult::Unchanged,
216218
_ => unreachable!(),
217219
},
@@ -222,7 +224,7 @@ fn success_in_grandchildren() {
222224

223225
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
224226
|obligation| match *obligation {
225-
"A.2.i.a" => ProcessResult::Changed(vec![]),
227+
"A.2.i.a" => ProcessResult::Changed(thin_vec![]),
226228
_ => unreachable!(),
227229
},
228230
|_| {},
@@ -247,7 +249,7 @@ fn to_errors_no_throw() {
247249
forest.register_obligation("A");
248250
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
249251
|obligation| match *obligation {
250-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
252+
"A" => ProcessResult::Changed(thin_vec!["A.1", "A.2", "A.3"]),
251253
"A.1" | "A.2" | "A.3" => ProcessResult::Unchanged,
252254
_ => unreachable!(),
253255
},
@@ -269,7 +271,7 @@ fn diamond() {
269271
forest.register_obligation("A");
270272
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
271273
|obligation| match *obligation {
272-
"A" => ProcessResult::Changed(vec!["A.1", "A.2"]),
274+
"A" => ProcessResult::Changed(thin_vec!["A.1", "A.2"]),
273275
"A.1" | "A.2" => ProcessResult::Unchanged,
274276
_ => unreachable!(),
275277
},
@@ -280,8 +282,8 @@ fn diamond() {
280282

281283
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
282284
|obligation| match *obligation {
283-
"A.1" => ProcessResult::Changed(vec!["D"]),
284-
"A.2" => ProcessResult::Changed(vec!["D"]),
285+
"A.1" => ProcessResult::Changed(thin_vec!["D"]),
286+
"A.2" => ProcessResult::Changed(thin_vec!["D"]),
285287
"D" => ProcessResult::Unchanged,
286288
_ => unreachable!(),
287289
},
@@ -295,7 +297,7 @@ fn diamond() {
295297
|obligation| match *obligation {
296298
"D" => {
297299
d_count += 1;
298-
ProcessResult::Changed(vec![])
300+
ProcessResult::Changed(thin_vec![])
299301
}
300302
_ => unreachable!(),
301303
},
@@ -313,7 +315,7 @@ fn diamond() {
313315
forest.register_obligation("A'");
314316
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
315317
|obligation| match *obligation {
316-
"A'" => ProcessResult::Changed(vec!["A'.1", "A'.2"]),
318+
"A'" => ProcessResult::Changed(thin_vec!["A'.1", "A'.2"]),
317319
"A'.1" | "A'.2" => ProcessResult::Unchanged,
318320
_ => unreachable!(),
319321
},
@@ -324,8 +326,8 @@ fn diamond() {
324326

325327
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
326328
|obligation| match *obligation {
327-
"A'.1" => ProcessResult::Changed(vec!["D'", "A'"]),
328-
"A'.2" => ProcessResult::Changed(vec!["D'"]),
329+
"A'.1" => ProcessResult::Changed(thin_vec!["D'", "A'"]),
330+
"A'.2" => ProcessResult::Changed(thin_vec!["D'"]),
329331
"D'" | "A'" => ProcessResult::Unchanged,
330332
_ => unreachable!(),
331333
},
@@ -366,7 +368,7 @@ fn done_dependency() {
366368

367369
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
368370
|obligation| match *obligation {
369-
"A: Sized" | "B: Sized" | "C: Sized" => ProcessResult::Changed(vec![]),
371+
"A: Sized" | "B: Sized" | "C: Sized" => ProcessResult::Changed(thin_vec![]),
370372
_ => unreachable!(),
371373
},
372374
|_| {},
@@ -379,7 +381,9 @@ fn done_dependency() {
379381
forest.register_obligation("(A,B,C): Sized");
380382
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
381383
|obligation| match *obligation {
382-
"(A,B,C): Sized" => ProcessResult::Changed(vec!["A: Sized", "B: Sized", "C: Sized"]),
384+
"(A,B,C): Sized" => {
385+
ProcessResult::Changed(thin_vec!["A: Sized", "B: Sized", "C: Sized"])
386+
}
383387
_ => unreachable!(),
384388
},
385389
|_| {},
@@ -399,10 +403,10 @@ fn orphan() {
399403

400404
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
401405
|obligation| match *obligation {
402-
"A" => ProcessResult::Changed(vec!["D", "E"]),
406+
"A" => ProcessResult::Changed(thin_vec!["D", "E"]),
403407
"B" => ProcessResult::Unchanged,
404-
"C1" => ProcessResult::Changed(vec![]),
405-
"C2" => ProcessResult::Changed(vec![]),
408+
"C1" => ProcessResult::Changed(thin_vec![]),
409+
"C2" => ProcessResult::Changed(thin_vec![]),
406410
"D" | "E" => ProcessResult::Unchanged,
407411
_ => unreachable!(),
408412
},
@@ -416,7 +420,7 @@ fn orphan() {
416420
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
417421
|obligation| match *obligation {
418422
"D" | "E" => ProcessResult::Unchanged,
419-
"B" => ProcessResult::Changed(vec!["D"]),
423+
"B" => ProcessResult::Changed(thin_vec!["D"]),
420424
_ => unreachable!(),
421425
},
422426
|_| {},
@@ -459,7 +463,7 @@ fn simultaneous_register_and_error() {
459463
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
460464
|obligation| match *obligation {
461465
"A" => ProcessResult::Error("An error"),
462-
"B" => ProcessResult::Changed(vec!["A"]),
466+
"B" => ProcessResult::Changed(thin_vec!["A"]),
463467
_ => unreachable!(),
464468
},
465469
|_| {},
@@ -474,7 +478,7 @@ fn simultaneous_register_and_error() {
474478
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
475479
|obligation| match *obligation {
476480
"A" => ProcessResult::Error("An error"),
477-
"B" => ProcessResult::Changed(vec!["A"]),
481+
"B" => ProcessResult::Changed(thin_vec!["A"]),
478482
_ => unreachable!(),
479483
},
480484
|_| {},

compiler/rustc_infer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ rustc_span = { path = "../rustc_span" }
2121
rustc_target = { path = "../rustc_target" }
2222
rustc_type_ir = { path = "../rustc_type_ir" }
2323
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
24+
thin-vec = "0.2.12"
2425
tracing = "0.1"
2526
# tidy-alphabetical-end

compiler/rustc_infer/src/traits/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::traits::solve::Certainty;
1717
pub use rustc_middle::traits::*;
1818
use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
1919
use rustc_span::Span;
20+
use thin_vec::ThinVec;
2021

2122
pub use self::ImplSource::*;
2223
pub use self::SelectionError::*;
@@ -84,7 +85,7 @@ pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
8485
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::TraitPredicate<'tcx>>;
8586
pub type PolyTraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
8687

87-
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
88+
pub type PredicateObligations<'tcx> = ThinVec<PredicateObligation<'tcx>>;
8889

8990
impl<'tcx> PredicateObligation<'tcx> {
9091
/// Flips the polarity of the inner predicate.

compiler/rustc_middle/src/traits/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_span::{DUMMY_SP, Span};
2525
// FIXME: Remove this import and import via `solve::`
2626
pub use rustc_type_ir::solve::{BuiltinImplSource, Reveal};
2727
use smallvec::{SmallVec, smallvec};
28+
use thin_vec::ThinVec;
2829

2930
pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache};
3031
use crate::mir::ConstraintCategory;
@@ -625,14 +626,14 @@ pub enum ImplSource<'tcx, N> {
625626
/// for some type parameter. The `Vec<N>` represents the
626627
/// obligations incurred from normalizing the where-clause (if
627628
/// any).
628-
Param(Vec<N>),
629+
Param(ThinVec<N>),
629630

630631
/// Successful resolution for a builtin impl.
631-
Builtin(BuiltinImplSource, Vec<N>),
632+
Builtin(BuiltinImplSource, ThinVec<N>),
632633
}
633634

634635
impl<'tcx, N> ImplSource<'tcx, N> {
635-
pub fn nested_obligations(self) -> Vec<N> {
636+
pub fn nested_obligations(self) -> ThinVec<N> {
636637
match self {
637638
ImplSource::UserDefined(i) => i.nested,
638639
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
@@ -686,7 +687,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
686687
pub struct ImplSourceUserDefinedData<'tcx, N> {
687688
pub impl_def_id: DefId,
688689
pub args: GenericArgsRef<'tcx>,
689-
pub nested: Vec<N>,
690+
pub nested: ThinVec<N>,
690691
}
691692

692693
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]

compiler/rustc_trait_selection/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ rustc_target = { path = "../rustc_target" }
2626
rustc_transmute = { path = "../rustc_transmute", features = ["rustc"] }
2727
rustc_type_ir = { path = "../rustc_type_ir" }
2828
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
29+
thin-vec = "0.2"
2930
tracing = "0.1"
3031
# tidy-alphabetical-end

compiler/rustc_trait_selection/src/solve/fulfill.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22
use std::mem;
33
use std::ops::ControlFlow;
44

5+
use rustc_data_structures::thinvec::ExtractIf;
56
use rustc_infer::infer::InferCtxt;
67
use rustc_infer::traits::query::NoSolution;
78
use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause};
@@ -81,7 +82,8 @@ impl<'tcx> ObligationStorage<'tcx> {
8182
// we get all obligations involved in the overflow. We pretty much check: if
8283
// we were to do another step of `select_where_possible`, which goals would
8384
// change.
84-
self.overflowed.extend(self.pending.extract_if(|o| {
85+
// FIXME: <https://github.com/Gankra/thin-vec/pull/66> is merged, this can be removed.
86+
self.overflowed.extend(ExtractIf::new(&mut self.pending, |o| {
8587
let goal = o.clone().into();
8688
let result = <&SolverDelegate<'tcx>>::from(infcx)
8789
.evaluate_root_goal(goal, GenerateProofTree::No)

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::mir::interpret::ErrorHandled;
1414
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1515
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1616
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt};
17+
use thin_vec::ThinVec;
1718
use tracing::{debug, debug_span, instrument};
1819

1920
use super::project::{self, ProjectAndUnifyResult};
@@ -28,7 +29,7 @@ use crate::traits::normalize::normalize_with_depth_to;
2829
use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _};
2930
use crate::traits::query::evaluate_obligation::InferCtxtExt;
3031

31-
pub(crate) type PendingPredicateObligations<'tcx> = Vec<PendingPredicateObligation<'tcx>>;
32+
pub(crate) type PendingPredicateObligations<'tcx> = ThinVec<PendingPredicateObligation<'tcx>>;
3233

3334
impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
3435
/// Note that we include both the `ParamEnv` and the `Predicate`,

compiler/rustc_type_ir/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_serialize = { path = "../rustc_serialize", optional = true }
1717
rustc_span = { path = "../rustc_span", optional = true }
1818
rustc_type_ir_macros = { path = "../rustc_type_ir_macros" }
1919
smallvec = { version = "1.8.1", default-features = false }
20+
thin-vec = "0.2.12"
2021
tracing = "0.1"
2122
# tidy-alphabetical-end
2223

@@ -30,7 +31,7 @@ nightly = [
3031
"smallvec/may_dangle",
3132
"smallvec/union",
3233
"rustc_index/nightly",
33-
"rustc_ast_ir/nightly"
34+
"rustc_ast_ir/nightly",
3435
]
3536

3637
[lints.rust]

0 commit comments

Comments
 (0)