Skip to content

Commit c83dbc5

Browse files
authored
Rollup merge of rust-lang#111246 - lcnr:no-escaping-bound-vars, r=compiler-errors
forbid escaping bound vars in combine removes the `CollectAllMismatches` in favor of a slightly more manual approach. r? types cc `@estebank`
2 parents d7a91f0 + 6691c4c commit c83dbc5

File tree

4 files changed

+46
-131
lines changed

4 files changed

+46
-131
lines changed

compiler/rustc_infer/src/infer/combine.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ impl<'tcx> InferCtxt<'tcx> {
7373
R: ObligationEmittingRelation<'tcx>,
7474
{
7575
let a_is_expected = relation.a_is_expected();
76+
debug_assert!(!a.has_escaping_bound_vars());
77+
debug_assert!(!b.has_escaping_bound_vars());
7678

7779
match (a.kind(), b.kind()) {
7880
// Relate integral variables to other types
@@ -163,6 +165,8 @@ impl<'tcx> InferCtxt<'tcx> {
163165
R: ObligationEmittingRelation<'tcx>,
164166
{
165167
debug!("{}.consts({:?}, {:?})", relation.tag(), a, b);
168+
debug_assert!(!a.has_escaping_bound_vars());
169+
debug_assert!(!b.has_escaping_bound_vars());
166170
if a == b {
167171
return Ok(a);
168172
}
@@ -238,22 +242,12 @@ impl<'tcx> InferCtxt<'tcx> {
238242
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
239243
return self.unify_const_variable(vid, a);
240244
}
241-
(ty::ConstKind::Unevaluated(..), _) if self.tcx.lazy_normalization() => {
242-
// FIXME(#59490): Need to remove the leak check to accommodate
243-
// escaping bound variables here.
244-
if !a.has_escaping_bound_vars() && !b.has_escaping_bound_vars() {
245-
relation.register_const_equate_obligation(a, b);
246-
}
245+
(ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
246+
if self.tcx.lazy_normalization() =>
247+
{
248+
relation.register_const_equate_obligation(a, b);
247249
return Ok(b);
248250
}
249-
(_, ty::ConstKind::Unevaluated(..)) if self.tcx.lazy_normalization() => {
250-
// FIXME(#59490): Need to remove the leak check to accommodate
251-
// escaping bound variables here.
252-
if !a.has_escaping_bound_vars() && !b.has_escaping_bound_vars() {
253-
relation.register_const_equate_obligation(a, b);
254-
}
255-
return Ok(a);
256-
}
257251
_ => {}
258252
}
259253

compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs

-102
This file was deleted.

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod ambiguity;
2-
pub mod method_chain;
32
pub mod on_unimplemented;
43
pub mod suggestions;
54

@@ -559,6 +558,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
559558
suggest_increasing_limit,
560559
|err| {
561560
self.note_obligation_cause_code(
561+
obligation.cause.body_id,
562562
err,
563563
predicate,
564564
obligation.param_env,
@@ -1431,6 +1431,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
14311431
| ObligationCauseCode::ExprItemObligation(..) = code
14321432
{
14331433
self.note_obligation_cause_code(
1434+
error.obligation.cause.body_id,
14341435
&mut diag,
14351436
error.obligation.predicate,
14361437
error.obligation.param_env,
@@ -2544,6 +2545,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25442545
// message, and fall back to regular note otherwise.
25452546
if !self.maybe_note_obligation_cause_for_async_await(err, obligation) {
25462547
self.note_obligation_cause_code(
2548+
obligation.cause.body_id,
25472549
err,
25482550
obligation.predicate,
25492551
obligation.param_env,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
2525
use rustc_hir::{Expr, HirId};
2626
use rustc_infer::infer::error_reporting::TypeErrCtxt;
2727
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
28-
use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime};
28+
use rustc_infer::infer::{DefineOpaqueTypes, InferOk, LateBoundRegionConversionTime};
2929
use rustc_middle::hir::map;
3030
use rustc_middle::ty::error::TypeError::{self, Sorts};
31-
use rustc_middle::ty::relate::TypeRelation;
3231
use rustc_middle::ty::{
3332
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind,
3433
GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, InternalSubsts,
@@ -39,9 +38,9 @@ use rustc_span::def_id::LocalDefId;
3938
use rustc_span::symbol::{sym, Ident, Symbol};
4039
use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP};
4140
use rustc_target::spec::abi;
41+
use std::iter;
4242
use std::ops::Deref;
4343

44-
use super::method_chain::CollectAllMismatches;
4544
use super::InferCtxtPrivExt;
4645
use crate::infer::InferCtxtExt as _;
4746
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
@@ -319,6 +318,7 @@ pub trait TypeErrCtxtExt<'tcx> {
319318

320319
fn note_obligation_cause_code<T>(
321320
&self,
321+
body_id: LocalDefId,
322322
err: &mut Diagnostic,
323323
predicate: T,
324324
param_env: ty::ParamEnv<'tcx>,
@@ -359,8 +359,9 @@ pub trait TypeErrCtxtExt<'tcx> {
359359
);
360360
fn note_function_argument_obligation(
361361
&self,
362-
arg_hir_id: HirId,
362+
body_id: LocalDefId,
363363
err: &mut Diagnostic,
364+
arg_hir_id: HirId,
364365
parent_code: &ObligationCauseCode<'tcx>,
365366
param_env: ty::ParamEnv<'tcx>,
366367
predicate: ty::Predicate<'tcx>,
@@ -2742,6 +2743,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
27422743
// bound that introduced the obligation (e.g. `T: Send`).
27432744
debug!(?next_code);
27442745
self.note_obligation_cause_code(
2746+
obligation.cause.body_id,
27452747
err,
27462748
obligation.predicate,
27472749
obligation.param_env,
@@ -2753,6 +2755,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
27532755

27542756
fn note_obligation_cause_code<T>(
27552757
&self,
2758+
body_id: LocalDefId,
27562759
err: &mut Diagnostic,
27572760
predicate: T,
27582761
param_env: ty::ParamEnv<'tcx>,
@@ -3152,6 +3155,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31523155
// #74711: avoid a stack overflow
31533156
ensure_sufficient_stack(|| {
31543157
self.note_obligation_cause_code(
3158+
body_id,
31553159
err,
31563160
parent_predicate,
31573161
param_env,
@@ -3163,6 +3167,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31633167
} else {
31643168
ensure_sufficient_stack(|| {
31653169
self.note_obligation_cause_code(
3170+
body_id,
31663171
err,
31673172
parent_predicate,
31683173
param_env,
@@ -3292,6 +3297,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32923297
// #74711: avoid a stack overflow
32933298
ensure_sufficient_stack(|| {
32943299
self.note_obligation_cause_code(
3300+
body_id,
32953301
err,
32963302
parent_predicate,
32973303
param_env,
@@ -3307,6 +3313,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
33073313
// #74711: avoid a stack overflow
33083314
ensure_sufficient_stack(|| {
33093315
self.note_obligation_cause_code(
3316+
body_id,
33103317
err,
33113318
parent_predicate,
33123319
param_env,
@@ -3323,15 +3330,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
33233330
..
33243331
} => {
33253332
self.note_function_argument_obligation(
3326-
arg_hir_id,
3333+
body_id,
33273334
err,
3335+
arg_hir_id,
33283336
parent_code,
33293337
param_env,
33303338
predicate,
33313339
call_hir_id,
33323340
);
33333341
ensure_sufficient_stack(|| {
33343342
self.note_obligation_cause_code(
3343+
body_id,
33353344
err,
33363345
predicate,
33373346
param_env,
@@ -3553,8 +3562,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35533562
}
35543563
fn note_function_argument_obligation(
35553564
&self,
3556-
arg_hir_id: HirId,
3565+
body_id: LocalDefId,
35573566
err: &mut Diagnostic,
3567+
arg_hir_id: HirId,
35583568
parent_code: &ObligationCauseCode<'tcx>,
35593569
param_env: ty::ParamEnv<'tcx>,
35603570
failed_pred: ty::Predicate<'tcx>,
@@ -3587,7 +3597,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35873597
// to an associated type (as seen from `trait_pred`) in the predicate. Like in
35883598
// trait_pred `S: Sum<<Self as Iterator>::Item>` and predicate `i32: Sum<&()>`
35893599
let mut type_diffs = vec![];
3590-
35913600
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref()
35923601
&& let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id)
35933602
&& let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs)
@@ -3596,14 +3605,26 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35963605
if let Some(where_pred) = where_pred.to_opt_poly_trait_pred()
35973606
&& let Some(failed_pred) = failed_pred.to_opt_poly_trait_pred()
35983607
{
3599-
let mut c = CollectAllMismatches {
3600-
infcx: self.infcx,
3601-
param_env,
3602-
errors: vec![],
3608+
let where_pred = self.instantiate_binder_with_placeholders(where_pred);
3609+
let failed_pred = self.instantiate_binder_with_fresh_vars(
3610+
expr.span,
3611+
LateBoundRegionConversionTime::FnCall,
3612+
failed_pred
3613+
);
3614+
3615+
let zipped =
3616+
iter::zip(where_pred.trait_ref.substs, failed_pred.trait_ref.substs);
3617+
for (expected, actual) in zipped {
3618+
self.probe(|_| {
3619+
match self
3620+
.at(&ObligationCause::misc(expr.span, body_id), param_env)
3621+
.eq(DefineOpaqueTypes::No, expected, actual)
3622+
{
3623+
Ok(_) => (), // We ignore nested obligations here for now.
3624+
Err(err) => type_diffs.push(err),
3625+
}
3626+
})
36033627
};
3604-
if let Ok(_) = c.relate(where_pred, failed_pred) {
3605-
type_diffs = c.errors;
3606-
}
36073628
} else if let Some(where_pred) = where_pred.to_opt_poly_projection_pred()
36083629
&& let Some(failed_pred) = failed_pred.to_opt_poly_projection_pred()
36093630
&& let Some(found) = failed_pred.skip_binder().term.ty()

0 commit comments

Comments
 (0)