Skip to content

Commit d64de46

Browse files
committed
Auto merge of #164216 - JonathanBrouwer:rollup-YK9Ft3h, r=JonathanBrouwer
Rollup of 8 pull requests Successful merges: - #164196 (`rust-analyzer` subtree update) - #164037 (Restrict application of optimization attributes from instance) - #164201 (Improve `--bless` output for `tests/debuginfo`) - #164202 ([rustdoc] Move the creation of `IndividualTestOptions` into the test runner closure to ensure the temporary folder is removed right after the test is done running) - #162140 (fn_addr_eq: we actually can guarantee basically nothing) - #163946 (Fix transmutability type mismatch bug) - #163978 (Copy over some methods from At to InferCtxt) - #164045 (-Zassumptions-on-binders: replace region vars with bound vars when handling alias outlives')
2 parents 0093837 + 5bbae1e commit d64de46

241 files changed

Lines changed: 7505 additions & 3201 deletions

File tree

Some content is hidden

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

‎compiler/rustc_infer/src/infer/at.rs‎

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
use relate::lattice::{LatticeOp, LatticeOpKind};
2929
use rustc_middle::ty::relate::solver_relating::RelateExt as NextSolverRelate;
30-
use rustc_middle::ty::{Const, TypingMode};
30+
use rustc_middle::ty::{Const, ParamEnv, TypingMode};
3131
use rustc_span::bug;
3232

3333
use super::*;
@@ -124,6 +124,143 @@ impl<'tcx> InferCtxt<'tcx> {
124124
forked.inner.borrow_mut().projection_cache().clear();
125125
forked
126126
}
127+
128+
/// Makes `actual <: expected`. For example, if type-checking a
129+
/// call like `foo(x)`, where `foo: fn(i32)`, you might have
130+
/// `sup(i32, x)`, since the "expected" type is the type that
131+
/// appears in the signature.
132+
///
133+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
134+
/// `FnCtxt`, or `TypeChecker`.
135+
pub fn sup_raw<T>(
136+
self,
137+
cause: &ObligationCause<'tcx>,
138+
param_env: ParamEnv<'tcx>,
139+
define_opaque_types: DefineOpaqueTypes,
140+
expected: T,
141+
actual: T,
142+
) -> InferResult<'tcx, ()>
143+
where
144+
T: ToTrace<'tcx>,
145+
{
146+
self.at(cause, param_env).sup(define_opaque_types, expected, actual)
147+
}
148+
149+
/// Makes `expected <: actual`.
150+
///
151+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
152+
/// `FnCtxt`, or `TypeChecker`.
153+
pub fn sub_raw<T>(
154+
self,
155+
cause: &ObligationCause<'tcx>,
156+
param_env: ParamEnv<'tcx>,
157+
define_opaque_types: DefineOpaqueTypes,
158+
expected: T,
159+
actual: T,
160+
) -> InferResult<'tcx, ()>
161+
where
162+
T: ToTrace<'tcx>,
163+
{
164+
self.at(cause, param_env).sub(define_opaque_types, expected, actual)
165+
}
166+
167+
/// Makes `expected == actual`.
168+
///
169+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
170+
/// `FnCtxt`, or `TypeChecker`.
171+
pub fn eq_raw<T>(
172+
self,
173+
cause: &ObligationCause<'tcx>,
174+
param_env: ParamEnv<'tcx>,
175+
define_opaque_types: DefineOpaqueTypes,
176+
expected: T,
177+
actual: T,
178+
) -> InferResult<'tcx, ()>
179+
where
180+
T: ToTrace<'tcx>,
181+
{
182+
self.eq_trace_raw(
183+
cause,
184+
param_env,
185+
define_opaque_types,
186+
ToTrace::to_trace(cause, expected, actual),
187+
expected,
188+
actual,
189+
)
190+
}
191+
192+
/// Makes `expected == actual`.
193+
///
194+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
195+
/// `FnCtxt`, or `TypeChecker`.
196+
pub fn eq_trace_raw<T>(
197+
self,
198+
cause: &ObligationCause<'tcx>,
199+
param_env: ParamEnv<'tcx>,
200+
define_opaque_types: DefineOpaqueTypes,
201+
trace: TypeTrace<'tcx>,
202+
expected: T,
203+
actual: T,
204+
) -> InferResult<'tcx, ()>
205+
where
206+
T: Relate<TyCtxt<'tcx>>,
207+
{
208+
self.at(cause, param_env).eq_trace(define_opaque_types, trace, expected, actual)
209+
}
210+
211+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
212+
/// `FnCtxt`, or `TypeChecker`.
213+
pub fn relate_raw<T>(
214+
self,
215+
cause: &ObligationCause<'tcx>,
216+
param_env: ParamEnv<'tcx>,
217+
define_opaque_types: DefineOpaqueTypes,
218+
expected: T,
219+
variance: ty::Variance,
220+
actual: T,
221+
) -> InferResult<'tcx, ()>
222+
where
223+
T: ToTrace<'tcx>,
224+
{
225+
self.at(cause, param_env).relate(define_opaque_types, expected, variance, actual)
226+
}
227+
228+
/// Computes the least-upper-bound, or mutual supertype, of two
229+
/// values. The order of the arguments doesn't matter, but since
230+
/// this can result in an error (e.g., if asked to compute LUB of
231+
/// u32 and i32), it is meaningful to call one of them the
232+
/// "expected type".
233+
///
234+
/// If possible, prefer the wrapper methods on `ObligationCtxt`,
235+
/// `FnCtxt`, or `TypeChecker`.
236+
pub fn lub_raw<T>(
237+
self,
238+
cause: &ObligationCause<'tcx>,
239+
param_env: ParamEnv<'tcx>,
240+
expected: T,
241+
actual: T,
242+
) -> InferResult<'tcx, T>
243+
where
244+
T: ToTrace<'tcx>,
245+
{
246+
self.at(cause, param_env).lub(expected, actual)
247+
}
248+
249+
pub fn goals_to_obligations(
250+
&self,
251+
cause: &ObligationCause<'tcx>,
252+
goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
253+
) -> InferOk<'tcx, ()> {
254+
InferOk {
255+
value: (),
256+
obligations: goals
257+
.into_iter()
258+
.map(|goal| {
259+
Obligation::new(self.tcx, cause.clone(), goal.param_env, goal.predicate)
260+
})
261+
.collect(),
262+
}
263+
}
127264
}
128265

129266
pub trait ToTrace<'tcx>: Relate<TyCtxt<'tcx>> + Copy {
@@ -135,6 +272,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
135272
/// call like `foo(x)`, where `foo: fn(i32)`, you might have
136273
/// `sup(i32, x)`, since the "expected" type is the type that
137274
/// appears in the signature.
275+
///
276+
/// FIXME: To be removed in favor of the [`InferCtxt::sup_raw`] method instead.
138277
pub fn sup<T>(
139278
self,
140279
define_opaque_types: DefineOpaqueTypes,
@@ -168,6 +307,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
168307
}
169308

170309
/// Makes `expected <: actual`.
310+
///
311+
/// FIXME: To be removed in favor of the [`InferCtxt::sub_raw`] method instead.
171312
pub fn sub<T>(
172313
self,
173314
define_opaque_types: DefineOpaqueTypes,
@@ -201,6 +342,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
201342
}
202343

203344
/// Makes `expected == actual`.
345+
///
346+
/// FIXME: To be removed in favor of the [`InferCtxt::eq_raw`] method instead.
204347
pub fn eq<T>(
205348
self,
206349
define_opaque_types: DefineOpaqueTypes,
@@ -219,6 +362,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
219362
}
220363

221364
/// Makes `expected == actual`.
365+
///
366+
/// FIXME: To be removed in favor of the [`InferCtxt::eq_trace_raw`] method instead.
222367
pub fn eq_trace<T>(
223368
self,
224369
define_opaque_types: DefineOpaqueTypes,
@@ -252,6 +397,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
252397
}
253398
}
254399

400+
/// FIXME: To be removed in favor of the [`InferCtxt::relate_raw`] method instead.
255401
pub fn relate<T>(
256402
self,
257403
define_opaque_types: DefineOpaqueTypes,
@@ -281,6 +427,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
281427
/// this can result in an error (e.g., if asked to compute LUB of
282428
/// u32 and i32), it is meaningful to call one of them the
283429
/// "expected type".
430+
///
431+
/// FIXME: To be removed in favor of the [`InferCtxt::lub_raw`] method instead.
284432
pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
285433
where
286434
T: ToTrace<'tcx>,
@@ -295,6 +443,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
295443
Ok(InferOk { value, obligations: op.into_obligations() })
296444
}
297445

446+
/// FIXME: To be removed in favor of the [`InferCtxt::goals_to_obligations`] method instead.
298447
fn goals_to_obligations(
299448
&self,
300449
goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,

‎compiler/rustc_middle/src/ty/context/impl_interner.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ bidirectional_lang_item_map! {
796796
FnMut,
797797
FnOnce,
798798
FnPtrTrait,
799+
Freeze,
799800
FusedIterator,
800801
Future,
801802
Iterator,

‎compiler/rustc_next_trait_solver/src/delegate.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ use std::fmt::Debug;
22
use std::ops::Deref;
33

44
use rustc_type_ir::solve::{
5-
Certainty, ComputeGoalFastPathOutcome, FetchEligibleAssocItemResponse, Goal, NoSolution,
5+
ComputeGoalFastPathOutcome, FetchEligibleAssocItemResponse, Goal, NoSolution,
66
VisibleForLeakCheck,
77
};
8-
use rustc_type_ir::{self as ty, CanonicalizerState, Const, InferCtxtLike, Interner, TypeFoldable};
8+
use rustc_type_ir::transmute::Answer;
9+
use rustc_type_ir::{
10+
self as ty, CanonicalizerState, Const, InferCtxtLike, Interner, Region, TypeFoldable,
11+
};
912

1013
/// `SolverDelegate` is one of the two traits in the `rustc_type_ir` shared abstraction layer
1114
/// between rustc and rust-analyzer abstracting over the [InferCtxt][inferctxt-doc], which had to be
@@ -113,7 +116,7 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
113116
src: <Self::Interner as Interner>::Ty,
114117
dst: <Self::Interner as Interner>::Ty,
115118
assume: Const<Self::Interner>,
116-
) -> Result<Certainty, NoSolution>;
119+
) -> Result<Answer<Region<Self::Interner>, <Self::Interner as Interner>::Ty>, NoSolution>;
117120

118121
/// Obtain canonicalizer state, either by allocating it afresh (the default) or by reusing
119122
/// previously allocated state.

‎compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_type_ir::solve::{
1616
NoSolutionOrRerunNonErased, OpaqueTypesJank, QueryResultOrRerunNonErased, RerunCondition,
1717
RerunNonErased, RerunReason, RerunResultExt, SmallCopySet, TyOrConstInferVar,
1818
};
19+
use rustc_type_ir::transmute::Answer;
1920
use rustc_type_ir::{
2021
self as ty, ClauseKind, Const, InferCtxtLike, Interner, MayBeErased, OpaqueTypeKey,
2122
PredicateKind, PredicateProxy, Region, RegionVid, TypeFoldable, TypeSuperVisitable,
@@ -1565,7 +1566,7 @@ where
15651566
src: I::Ty,
15661567
dst: I::Ty,
15671568
assume: Const<I>,
1568-
) -> Result<Certainty, NoSolution> {
1569+
) -> Result<Answer<Region<I>, I::Ty>, NoSolution> {
15691570
self.delegate.is_transmutable(dst, src, assume)
15701571
}
15711572

‎compiler/rustc_next_trait_solver/src/solve/mod.rs‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ mod trait_goals;
2222

2323
use derive_where::derive_where;
2424
use rustc_type_ir::inherent::*;
25+
use rustc_type_ir::lang_items::SolverTraitLangItem;
2526
pub use rustc_type_ir::solve::*;
26-
use rustc_type_ir::{self as ty, Const, Interner, Region, TypeVisitableExt};
27+
use rustc_type_ir::transmute::Condition;
28+
use rustc_type_ir::{self as ty, Const, Interner, Region, TraitClause, TypeVisitableExt, Upcast};
29+
use thin_vec::{ThinVec, thin_vec};
2730
use tracing::instrument;
2831

2932
pub use self::eval_ctxt::{
@@ -491,3 +494,42 @@ pub struct GoalEvaluation<I: Interner> {
491494
/// before rerunning it.
492495
pub stalled_on: Option<GoalStalledOn<I>>,
493496
}
497+
498+
/// Flatten the `Condition` tree into a conjunction of predicates.
499+
#[instrument(level = "debug", skip(interner, predicate))]
500+
pub fn flatten_answer_tree<I: Interner>(
501+
interner: I,
502+
predicate: TraitClause<I>,
503+
cond: Condition<Region<I>, I::Ty>,
504+
) -> ThinVec<I::Predicate> {
505+
match cond {
506+
// FIXME(bryangarza): Add separate `IfAny` case, instead of treating as `IfAll`
507+
// Not possible until the trait solver supports disjunctions of obligations
508+
Condition::IfAll(conds) | Condition::IfAny(conds) => conds
509+
.into_iter()
510+
.flat_map(|cond| flatten_answer_tree(interner, predicate, cond))
511+
.collect(),
512+
Condition::Immutable { ty } => {
513+
let trait_ref = ty::TraitRef::new(
514+
interner,
515+
interner.require_trait_lang_item(SolverTraitLangItem::Freeze),
516+
[I::GenericArg::from(ty)],
517+
);
518+
thin_vec![trait_ref.upcast(interner)]
519+
}
520+
Condition::Outlives { long, short } => {
521+
let outlives = ty::OutlivesClause(long, short);
522+
thin_vec![outlives.upcast(interner)]
523+
}
524+
Condition::Transmutable { src, dst } => {
525+
let transmute_trait = predicate.def_id();
526+
let assume = predicate.trait_ref.args.const_at(2);
527+
let trait_ref = ty::TraitRef::new(
528+
interner,
529+
transmute_trait,
530+
[I::GenericArg::from(dst), I::GenericArg::from(src), I::GenericArg::from(assume)],
531+
);
532+
thin_vec![trait_ref.upcast(interner)]
533+
}
534+
}
535+
}

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::solve::assembly::{
2424
use crate::solve::inspect::ProbeKind;
2525
use crate::solve::{
2626
BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
27-
MergeCandidateInfo, NoSolution, ParamEnvSource, StalledOnCoroutines,
27+
MergeCandidateInfo, NoSolution, ParamEnvSource, StalledOnCoroutines, flatten_answer_tree,
2828
has_only_region_constraints,
2929
};
3030

@@ -659,6 +659,8 @@ where
659659
ecx: &mut EvalCtxt<'_, D>,
660660
goal: Goal<I, Self>,
661661
) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
662+
use rustc_type_ir::transmute::Answer;
663+
662664
if goal.predicate.polarity != ty::ClausePolarity::Positive {
663665
return Err(NoSolution.into());
664666
}
@@ -681,11 +683,28 @@ where
681683
goal.predicate.trait_ref.args.const_at(2),
682684
)?;
683685

684-
let certainty = ecx.is_transmutable(
686+
let answer = ecx.is_transmutable(
685687
goal.predicate.trait_ref.args.type_at(0),
686688
goal.predicate.trait_ref.args.type_at(1),
687689
assume,
688690
)?;
691+
692+
let certainty = match answer {
693+
Answer::No(_) => return Err(NoSolution.into()),
694+
Answer::Yes => Certainty::Yes,
695+
Answer::If(cond) => {
696+
let tcx = ecx.cx();
697+
ecx.add_goals(
698+
GoalSource::Misc,
699+
flatten_answer_tree(tcx, goal.predicate, cond)
700+
.into_iter()
701+
.map(|predicate| Goal::new(tcx, goal.param_env, predicate)),
702+
)?;
703+
704+
Certainty::Yes
705+
}
706+
};
707+
689708
ecx.evaluate_added_goals_and_make_canonical_response(certainty)
690709
},
691710
)

0 commit comments

Comments
 (0)