Skip to content

Commit faab68e

Browse files
committed
Auto merge of rust-lang#103426 - matthiaskrgr:rollup-n6dqdy8, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - rust-lang#103123 (Introduce `subst_iter` and `subst_iter_copied` on `EarlyBinder` ) - rust-lang#103328 (Do not suggest trivially false const predicates) - rust-lang#103354 (Escape string literals when fixing overlong char literal) - rust-lang#103355 (Handle return-position `impl Trait` in traits properly in `register_hidden_type`) - rust-lang#103368 (Delay ambiguity span bug in normalize query iff not rustdoc) - rust-lang#103388 (rustdoc: remove unused CSS class `.result-description`) - rust-lang#103399 (Change `unknown_lint` applicability to `MaybeIncorrect`) - rust-lang#103401 (Use functions for headings rustdoc GUI test) - rust-lang#103412 (Fix typo in docs of `String::leak`.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6c9c2d8 + 25e02d6 commit faab68e

Some content is hidden

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

45 files changed

+471
-252
lines changed

compiler/rustc_hir_analysis/src/check/compare_method.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
664664
});
665665
self.types.insert(proj.item_def_id, (infer_ty, proj.substs));
666666
// Recurse into bounds
667-
for pred in self.tcx().bound_explicit_item_bounds(proj.item_def_id).transpose_iter() {
668-
let pred_span = pred.0.1;
669-
670-
let pred = pred.map_bound(|(pred, _)| *pred).subst(self.tcx(), proj.substs);
667+
for (pred, pred_span) in self.tcx().bound_explicit_item_bounds(proj.item_def_id).subst_iter_copied(self.tcx(), proj.substs) {
671668
let pred = pred.fold_with(self);
672669
let pred = self.ocx.normalize(
673670
ObligationCause::misc(self.span, self.body_id),
@@ -1752,15 +1749,10 @@ pub fn check_type_bounds<'tcx>(
17521749

17531750
let obligations = tcx
17541751
.bound_explicit_item_bounds(trait_ty.def_id)
1755-
.transpose_iter()
1756-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
1757-
.map(|(bound, span)| {
1758-
debug!(?bound);
1759-
// this is where opaque type is found
1760-
let concrete_ty_bound = bound.subst(tcx, rebased_substs);
1752+
.subst_iter_copied(tcx, rebased_substs)
1753+
.map(|(concrete_ty_bound, span)| {
17611754
debug!("check_type_bounds: concrete_ty_bound = {:?}", concrete_ty_bound);
1762-
1763-
traits::Obligation::new(mk_cause(span.0), param_env, concrete_ty_bound)
1755+
traits::Obligation::new(mk_cause(span), param_env, concrete_ty_bound)
17641756
})
17651757
.collect();
17661758
debug!("check_type_bounds: item_bounds={:?}", obligations);

compiler/rustc_hir_typeck/src/_match.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
514514
}
515515

516516
for ty in [first_ty, second_ty] {
517-
for pred in self.tcx.bound_explicit_item_bounds(rpit_def_id).transpose_iter() {
518-
let pred = pred.map_bound(|(pred, _)| *pred).subst(self.tcx, substs);
517+
for (pred, _) in self
518+
.tcx
519+
.bound_explicit_item_bounds(rpit_def_id)
520+
.subst_iter_copied(self.tcx, substs)
521+
{
519522
let pred = match pred.kind().skip_binder() {
520523
ty::PredicateKind::Trait(mut trait_pred) => {
521524
assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty);

compiler/rustc_hir_typeck/src/closure.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -176,24 +176,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
176176
match *expected_ty.kind() {
177177
ty::Opaque(def_id, substs) => {
178178
let bounds = self.tcx.bound_explicit_item_bounds(def_id);
179-
let sig = bounds
180-
.transpose_iter()
181-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
182-
.find_map(|(pred, span)| match pred.0.kind().skip_binder() {
179+
let sig =
180+
bounds.subst_iter_copied(self.tcx, substs).find_map(|(pred, span)| match pred
181+
.kind()
182+
.skip_binder()
183+
{
183184
ty::PredicateKind::Projection(proj_predicate) => self
184185
.deduce_sig_from_projection(
185-
Some(span.0),
186-
pred.0
187-
.kind()
188-
.rebind(pred.rebind(proj_predicate).subst(self.tcx, substs)),
186+
Some(span),
187+
pred.kind().rebind(proj_predicate),
189188
),
190189
_ => None,
191190
});
192191

193192
let kind = bounds
194-
.transpose_iter()
195-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
196-
.filter_map(|(pred, _)| match pred.0.kind().skip_binder() {
193+
.0
194+
.iter()
195+
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
197196
ty::PredicateKind::Trait(tp) => {
198197
self.tcx.fn_trait_kind_from_lang_item(tp.def_id())
199198
}
@@ -697,18 +696,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
697696
ty::Opaque(def_id, substs) => self
698697
.tcx
699698
.bound_explicit_item_bounds(def_id)
700-
.transpose_iter()
701-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
702-
.find_map(|(p, s)| get_future_output(p.subst(self.tcx, substs), s.0))?,
699+
.subst_iter_copied(self.tcx, substs)
700+
.find_map(|(p, s)| get_future_output(p, s))?,
703701
ty::Error(_) => return None,
704702
ty::Projection(proj)
705703
if self.tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder =>
706704
{
707705
self.tcx
708706
.bound_explicit_item_bounds(proj.item_def_id)
709-
.transpose_iter()
710-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
711-
.find_map(|(p, s)| get_future_output(p.subst(self.tcx, proj.substs), s.0))?
707+
.subst_iter_copied(self.tcx, proj.substs)
708+
.find_map(|(p, s)| get_future_output(p, s))?
712709
}
713710
_ => span_bug!(
714711
self.tcx.def_span(expr_def_id),

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ impl<'tcx> InferCtxt<'tcx> {
338338

339339
let bounds = self.tcx.bound_explicit_item_bounds(*def_id);
340340

341-
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
342-
let predicate = predicate.subst(self.tcx, substs);
341+
for (predicate, _) in bounds.subst_iter_copied(self.tcx, substs) {
343342
let output = predicate
344343
.kind()
345344
.map_bound(|kind| match kind {
@@ -2272,6 +2271,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22722271
struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str)
22732272
}
22742273
FailureCode::Error0308(failure_str) => {
2274+
fn escape_literal(s: &str) -> String {
2275+
let mut escaped = String::with_capacity(s.len());
2276+
let mut chrs = s.chars().peekable();
2277+
while let Some(first) = chrs.next() {
2278+
match (first, chrs.peek()) {
2279+
('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
2280+
escaped.push('\\');
2281+
escaped.push(delim);
2282+
chrs.next();
2283+
}
2284+
('"' | '\'', _) => {
2285+
escaped.push('\\');
2286+
escaped.push(first)
2287+
}
2288+
(c, _) => escaped.push(c),
2289+
};
2290+
}
2291+
escaped
2292+
}
22752293
let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str);
22762294
if let Some((expected, found)) = trace.values.ty() {
22772295
match (expected.kind(), found.kind()) {
@@ -2293,7 +2311,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22932311
err.span_suggestion(
22942312
span,
22952313
"if you meant to write a `char` literal, use single quotes",
2296-
format!("'{}'", code),
2314+
format!("'{}'", escape_literal(code)),
22972315
Applicability::MachineApplicable,
22982316
);
22992317
}
@@ -2308,7 +2326,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23082326
err.span_suggestion(
23092327
span,
23102328
"if you meant to write a `str` literal, use double quotes",
2311-
format!("\"{}\"", code),
2329+
format!("\"{}\"", escape_literal(code)),
23122330
Applicability::MachineApplicable,
23132331
);
23142332
}

compiler/rustc_infer/src/infer/opaque_types.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::errors::OpaqueHiddenTypeDiag;
22
use crate::infer::{DefiningAnchor, InferCtxt, InferOk};
33
use crate::traits;
4+
use hir::def::DefKind;
45
use hir::def_id::{DefId, LocalDefId};
56
use hir::{HirId, OpaqueTyOrigin};
67
use rustc_data_structures::sync::Lrc;
@@ -543,16 +544,18 @@ impl<'tcx> InferCtxt<'tcx> {
543544

544545
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());
545546

546-
for predicate in item_bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
547-
debug!(?predicate);
548-
let predicate = predicate.subst(tcx, substs);
549-
547+
for (predicate, _) in item_bounds.subst_iter_copied(tcx, substs) {
550548
let predicate = predicate.fold_with(&mut BottomUpFolder {
551549
tcx,
552550
ty_op: |ty| match *ty.kind() {
553551
// We can't normalize associated types from `rustc_infer`,
554552
// but we can eagerly register inference variables for them.
555-
ty::Projection(projection_ty) if !projection_ty.has_escaping_bound_vars() => {
553+
// FIXME(RPITIT): Don't replace RPITITs with inference vars.
554+
ty::Projection(projection_ty)
555+
if !projection_ty.has_escaping_bound_vars()
556+
&& tcx.def_kind(projection_ty.item_def_id)
557+
!= DefKind::ImplTraitPlaceholder =>
558+
{
556559
self.infer_projection(
557560
param_env,
558561
projection_ty,
@@ -568,6 +571,12 @@ impl<'tcx> InferCtxt<'tcx> {
568571
{
569572
hidden_ty
570573
}
574+
// FIXME(RPITIT): This can go away when we move to associated types
575+
ty::Projection(proj)
576+
if def_id.to_def_id() == proj.item_def_id && substs == proj.substs =>
577+
{
578+
hidden_ty
579+
}
571580
_ => ty,
572581
},
573582
lt_op: |lt| lt,

compiler/rustc_lint/src/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
960960
sp,
961961
"did you mean",
962962
suggestion,
963-
Applicability::MachineApplicable,
963+
Applicability::MaybeIncorrect,
964964
);
965965
}
966966
lint

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,12 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
9191
// For example, in `impl Trait<Assoc = impl Send>`, for all of the bounds on `Assoc`,
9292
// e.g. `type Assoc: OtherTrait`, replace `<impl Trait as Trait>::Assoc: OtherTrait`
9393
// with `impl Send: OtherTrait`.
94-
for assoc_pred_and_span in
95-
cx.tcx.bound_explicit_item_bounds(proj.projection_ty.item_def_id).transpose_iter()
94+
for (assoc_pred, assoc_pred_span) in cx
95+
.tcx
96+
.bound_explicit_item_bounds(proj.projection_ty.item_def_id)
97+
.subst_iter_copied(cx.tcx, &proj.projection_ty.substs)
9698
{
97-
let assoc_pred_span = assoc_pred_and_span.0.1;
98-
let assoc_pred = assoc_pred_and_span
99-
.map_bound(|(pred, _)| *pred)
100-
.subst(cx.tcx, &proj.projection_ty.substs)
101-
.fold_with(proj_replacer);
99+
let assoc_pred = assoc_pred.fold_with(proj_replacer);
102100
let Ok(assoc_pred) = traits::fully_normalize(infcx, traits::ObligationCause::dummy(), cx.param_env, assoc_pred) else {
103101
continue;
104102
};

compiler/rustc_middle/src/ty/print/pretty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,7 @@ pub trait PrettyPrinter<'tcx>:
795795
let mut fn_traits = FxIndexMap::default();
796796
let mut is_sized = false;
797797

798-
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
799-
let predicate = predicate.subst(tcx, substs);
798+
for (predicate, _) in bounds.subst_iter_copied(tcx, substs) {
800799
let bound_predicate = predicate.kind();
801800

802801
match bound_predicate.skip_binder() {

compiler/rustc_middle/src/ty/subst.rs

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
66
use crate::ty::visit::{TypeVisitable, TypeVisitor};
77
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
88

9+
use rustc_data_structures::captures::Captures;
910
use rustc_data_structures::intern::{Interned, WithStableHash};
1011
use rustc_hir::def_id::DefId;
1112
use rustc_macros::HashStable;
@@ -558,6 +559,28 @@ impl<T, U> EarlyBinder<(T, U)> {
558559
}
559560
}
560561

562+
impl<'tcx, 's, T: IntoIterator<Item = I>, I: TypeFoldable<'tcx>> EarlyBinder<T> {
563+
pub fn subst_iter(
564+
self,
565+
tcx: TyCtxt<'tcx>,
566+
substs: &'s [GenericArg<'tcx>],
567+
) -> impl Iterator<Item = I> + Captures<'s> + Captures<'tcx> {
568+
self.0.into_iter().map(move |t| EarlyBinder(t).subst(tcx, substs))
569+
}
570+
}
571+
572+
impl<'tcx, 's, 'a, T: IntoIterator<Item = &'a I>, I: Copy + TypeFoldable<'tcx> + 'a>
573+
EarlyBinder<T>
574+
{
575+
pub fn subst_iter_copied(
576+
self,
577+
tcx: TyCtxt<'tcx>,
578+
substs: &'s [GenericArg<'tcx>],
579+
) -> impl Iterator<Item = I> + Captures<'s> + Captures<'tcx> + Captures<'a> {
580+
self.0.into_iter().map(move |t| EarlyBinder(*t).subst(tcx, substs))
581+
}
582+
}
583+
561584
pub struct EarlyBinderIter<T> {
562585
t: T,
563586
}

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,26 @@ pub(crate) fn emit_unescape_error(
113113
} else {
114114
("", "if you meant to write a `str` literal, use double quotes")
115115
};
116-
116+
let mut escaped = String::with_capacity(lit.len());
117+
let mut chrs = lit.chars().peekable();
118+
while let Some(first) = chrs.next() {
119+
match (first, chrs.peek()) {
120+
('\\', Some('"')) => {
121+
escaped.push('\\');
122+
escaped.push('"');
123+
chrs.next();
124+
}
125+
('"', _) => {
126+
escaped.push('\\');
127+
escaped.push('"')
128+
}
129+
(c, _) => escaped.push(c),
130+
};
131+
}
117132
handler.span_suggestion(
118133
span_with_quotes,
119134
msg,
120-
format!("{}\"{}\"", prefix, lit),
135+
format!("{prefix}\"{escaped}\""),
121136
Applicability::MachineApplicable,
122137
);
123138
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
764764
self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty);
765765
}
766766

767+
let mut unsatisfied_const = false;
767768
if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
768769
let non_const_predicate = trait_ref.without_const();
769770
let non_const_obligation = Obligation {
@@ -773,6 +774,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
773774
recursion_depth: obligation.recursion_depth,
774775
};
775776
if self.predicate_may_hold(&non_const_obligation) {
777+
unsatisfied_const = true;
776778
err.span_note(
777779
span,
778780
&format!(
@@ -924,7 +926,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
924926
}
925927
}
926928
} else if !trait_ref.has_non_region_infer()
927-
&& self.predicate_can_apply(obligation.param_env, trait_ref)
929+
&& self.predicate_can_apply(obligation.param_env, trait_predicate)
928930
{
929931
// If a where-clause may be useful, remind the
930932
// user that they can add it.
@@ -939,7 +941,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
939941
None,
940942
obligation.cause.body_id,
941943
);
942-
} else if !suggested {
944+
} else if !suggested && !unsatisfied_const {
943945
// Can't show anything else useful, try to find similar impls.
944946
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
945947
if !self.report_similar_impl_candidates(
@@ -1436,7 +1438,7 @@ trait InferCtxtPrivExt<'tcx> {
14361438
fn predicate_can_apply(
14371439
&self,
14381440
param_env: ty::ParamEnv<'tcx>,
1439-
pred: ty::PolyTraitRef<'tcx>,
1441+
pred: ty::PolyTraitPredicate<'tcx>,
14401442
) -> bool;
14411443

14421444
fn note_obligation_cause(&self, err: &mut Diagnostic, obligation: &PredicateObligation<'tcx>);
@@ -2511,7 +2513,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25112513
fn predicate_can_apply(
25122514
&self,
25132515
param_env: ty::ParamEnv<'tcx>,
2514-
pred: ty::PolyTraitRef<'tcx>,
2516+
pred: ty::PolyTraitPredicate<'tcx>,
25152517
) -> bool {
25162518
struct ParamToVarFolder<'a, 'tcx> {
25172519
infcx: &'a InferCtxt<'tcx>,
@@ -2555,7 +2557,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25552557
let obligation = Obligation::new(
25562558
ObligationCause::dummy(),
25572559
param_env,
2558-
cleaned_pred.without_const().to_predicate(selcx.tcx()),
2560+
cleaned_pred.to_predicate(selcx.tcx()),
25592561
);
25602562

25612563
self.predicate_may_hold(&obligation)

0 commit comments

Comments
 (0)