Skip to content

Commit 2a5d257

Browse files
authored
Rollup merge of rust-lang#105674 - estebank:iterator-chains, r=oli-obk
Point at method chains on `E0271` errors Follow up to rust-lang#105332. Fix rust-lang#33941. CC rust-lang#9082. r? ``@oli-obk``
2 parents d2b716d + 7f8fdf4 commit 2a5d257

30 files changed

+300
-185
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
2525
use rustc_middle::traits::util::supertraits;
2626
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
2727
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
28-
use rustc_middle::ty::print::with_crate_prefix;
28+
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
2929
use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable};
3030
use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
3131
use rustc_span::symbol::{kw, sym, Ident};
@@ -270,7 +270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
270270
let tcx = self.tcx;
271271

272272
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
273-
let ty_str = self.ty_to_string(rcvr_ty);
273+
let ty_str = with_forced_trimmed_paths!(self.ty_to_string(rcvr_ty));
274274
let is_method = mode == Mode::MethodCall;
275275
let item_kind = if is_method {
276276
"method"
@@ -565,7 +565,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
565565
let term = pred.skip_binder().term;
566566

567567
let obligation = format!("{} = {}", projection_ty, term);
568-
let quiet = format!("{} = {}", quiet_projection_ty, term);
568+
let quiet = with_forced_trimmed_paths!(format!(
569+
"{} = {}",
570+
quiet_projection_ty, term
571+
));
569572

570573
bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
571574
Some((obligation, projection_ty.self_ty()))
@@ -575,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
575578
let self_ty = p.self_ty();
576579
let path = p.print_only_trait_path();
577580
let obligation = format!("{}: {}", self_ty, path);
578-
let quiet = format!("_: {}", path);
581+
let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
579582
bound_span_label(self_ty, &obligation, &quiet);
580583
Some((obligation, self_ty))
581584
}
@@ -798,7 +801,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
798801
(None, None)
799802
};
800803
let primary_message = primary_message.unwrap_or_else(|| format!(
801-
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, but its trait bounds were not satisfied"
804+
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, \
805+
but its trait bounds were not satisfied"
802806
));
803807
err.set_primary_message(&primary_message);
804808
if let Some(label) = label {
@@ -895,7 +899,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
895899
}
896900
}
897901
} else {
898-
err.span_label(span, format!("{item_kind} cannot be called on `{ty_str}` due to unsatisfied trait bounds"));
902+
err.span_label(
903+
span,
904+
format!(
905+
"{item_kind} cannot be called on `{ty_str}` due to unsatisfied \
906+
trait bounds",
907+
),
908+
);
899909
}
900910
};
901911

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::config::TrimmedDefPaths;
1616
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1717
use rustc_session::Limit;
1818
use rustc_span::symbol::{kw, Ident, Symbol};
19+
use rustc_span::FileNameDisplayPreference;
1920
use rustc_target::abi::Size;
2021
use rustc_target::spec::abi::Abi;
2122
use smallvec::SmallVec;
@@ -818,11 +819,16 @@ pub trait PrettyPrinter<'tcx>:
818819
p!("@", print_def_path(did.to_def_id(), substs));
819820
} else {
820821
let span = self.tcx().def_span(did);
822+
let preference = if FORCE_TRIMMED_PATH.with(|flag| flag.get()) {
823+
FileNameDisplayPreference::Short
824+
} else {
825+
FileNameDisplayPreference::Remapped
826+
};
821827
p!(write(
822828
"@{}",
823829
// This may end up in stderr diagnostics but it may also be emitted
824830
// into MIR. Hence we use the remapped path if available
825-
self.tcx().sess.source_map().span_to_embeddable_string(span)
831+
self.tcx().sess.source_map().span_to_string(span, preference)
826832
));
827833
}
828834
} else {

compiler/rustc_span/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ impl RealFileName {
259259
FileNameDisplayPreference::Remapped => {
260260
self.remapped_path_if_available().to_string_lossy()
261261
}
262+
FileNameDisplayPreference::Short => self
263+
.local_path_if_available()
264+
.file_name()
265+
.map_or_else(|| "".into(), |f| f.to_string_lossy()),
262266
}
263267
}
264268
}
@@ -302,6 +306,9 @@ pub enum FileNameDisplayPreference {
302306
/// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
303307
/// This is appropriate for use in user-facing output (such as diagnostics).
304308
Local,
309+
/// Display only the filename, as a way to reduce the verbosity of the output.
310+
/// This is appropriate for use in user-facing output (such as diagnostics).
311+
Short,
305312
}
306313

307314
pub struct FileNameDisplay<'a> {

compiler/rustc_span/src/source_map.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -438,20 +438,27 @@ impl SourceMap {
438438
}
439439
}
440440

441-
fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
441+
pub fn span_to_string(
442+
&self,
443+
sp: Span,
444+
filename_display_pref: FileNameDisplayPreference,
445+
) -> String {
442446
if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
443447
return "no-location".to_string();
444448
}
445449

446450
let lo = self.lookup_char_pos(sp.lo());
447451
let hi = self.lookup_char_pos(sp.hi());
448452
format!(
449-
"{}:{}:{}: {}:{}",
453+
"{}:{}:{}{}",
450454
lo.file.name.display(filename_display_pref),
451455
lo.line,
452456
lo.col.to_usize() + 1,
453-
hi.line,
454-
hi.col.to_usize() + 1,
457+
if let FileNameDisplayPreference::Short = filename_display_pref {
458+
String::new()
459+
} else {
460+
format!(": {}:{}", hi.line, hi.col.to_usize() + 1)
461+
}
455462
)
456463
}
457464

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_middle::traits::select::OverflowError;
3535
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
3636
use rustc_middle::ty::error::ExpectedFound;
3737
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
38-
use rustc_middle::ty::print::{FmtPrinter, Print};
38+
use rustc_middle::ty::print::{with_forced_trimmed_paths, FmtPrinter, Print};
3939
use rustc_middle::ty::{
4040
self, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeFoldable,
4141
TypeVisitable,
@@ -1757,21 +1757,26 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17571757
let trait_def_id = pred.projection_ty.trait_def_id(self.tcx);
17581758
let self_ty = pred.projection_ty.self_ty();
17591759

1760-
if Some(pred.projection_ty.def_id) == self.tcx.lang_items().fn_once_output() {
1761-
Some(format!(
1762-
"expected `{self_ty}` to be a {fn_kind} that returns `{expected_ty}`, but it returns `{normalized_ty}`",
1763-
fn_kind = self_ty.prefix_string(self.tcx)
1764-
))
1765-
} else if Some(trait_def_id) == self.tcx.lang_items().future_trait() {
1766-
Some(format!(
1767-
"expected `{self_ty}` to be a future that resolves to `{expected_ty}`, but it resolves to `{normalized_ty}`"
1768-
))
1769-
} else if Some(trait_def_id) == self.tcx.get_diagnostic_item(sym::Iterator) {
1770-
Some(format!(
1771-
"expected `{self_ty}` to be an iterator that yields `{expected_ty}`, but it yields `{normalized_ty}`"
1772-
))
1773-
} else {
1774-
None
1760+
with_forced_trimmed_paths! {
1761+
if Some(pred.projection_ty.def_id) == self.tcx.lang_items().fn_once_output() {
1762+
Some(format!(
1763+
"expected `{self_ty}` to be a {fn_kind} that returns `{expected_ty}`, but it \
1764+
returns `{normalized_ty}`",
1765+
fn_kind = self_ty.prefix_string(self.tcx)
1766+
))
1767+
} else if Some(trait_def_id) == self.tcx.lang_items().future_trait() {
1768+
Some(format!(
1769+
"expected `{self_ty}` to be a future that resolves to `{expected_ty}`, but it \
1770+
resolves to `{normalized_ty}`"
1771+
))
1772+
} else if Some(trait_def_id) == self.tcx.get_diagnostic_item(sym::Iterator) {
1773+
Some(format!(
1774+
"expected `{self_ty}` to be an iterator that yields `{expected_ty}`, but it \
1775+
yields `{normalized_ty}`"
1776+
))
1777+
} else {
1778+
None
1779+
}
17751780
}
17761781
}
17771782

0 commit comments

Comments
 (0)