Skip to content

Commit 1d72d7e

Browse files
committed
Auto merge of #156134 - jhpratt:rollup-79KaBC4, r=jhpratt
Rollup of 3 pull requests Successful merges: - #156120 (Regression test for trait-system-refactor#7) - #156125 (refactor using ExprParenthesesNeeded where possible) - #156132 (More lifting tweaks)
2 parents 783062d + 037e2b0 commit 1d72d7e

11 files changed

Lines changed: 78 additions & 56 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::ty::error::TypeError;
2121
use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, Unnormalized};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_session::Session;
24+
use rustc_session::errors::ExprParenthesesNeeded;
2425
use rustc_span::{DUMMY_SP, Ident, Span, kw, sym};
2526
use rustc_trait_selection::error_reporting::infer::{FailureCode, ObligationCauseExt};
2627
use rustc_trait_selection::infer::InferCtxtExt;
@@ -954,14 +955,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
954955
// wrapping in parentheses. We find the statement or expression
955956
// following the `match` (`&& true`) and see if it is something that
956957
// can reasonably be interpreted as a binop following an expression.
957-
err.multipart_suggestion(
958-
"parentheses are required to parse this as an expression",
959-
vec![
960-
(expr.span.shrink_to_lo(), "(".to_string()),
961-
(expr.span.shrink_to_hi(), ")".to_string()),
962-
],
963-
Applicability::MachineApplicable,
964-
);
958+
err.subdiagnostic(ExprParenthesesNeeded::surrounding(expr.span));
965959
} else if expr.can_have_side_effects() {
966960
self.suggest_semicolon_at_end(expr.span, err);
967961
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -799,14 +799,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799799
&& let hir::StmtKind::Expr(_) = stmt.kind
800800
&& self.is_next_stmt_expr_continuation(stmt.hir_id)
801801
{
802-
err.multipart_suggestion(
803-
"parentheses are required to parse this as an expression",
804-
vec![
805-
(stmt.span.shrink_to_lo(), "(".to_string()),
806-
(stmt.span.shrink_to_hi(), ")".to_string()),
807-
],
808-
Applicability::MachineApplicable,
809-
);
802+
err.subdiagnostic(ExprParenthesesNeeded::surrounding(stmt.span));
810803
} else {
811804
err.span_suggestion(
812805
expression.span.shrink_to_hi(),
@@ -843,14 +836,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
843836
// wrapping in parentheses. We find the statement or expression following the
844837
// `if` (`&& true`) and see if it is something that can reasonably be
845838
// interpreted as a binop following an expression.
846-
err.multipart_suggestion(
847-
"parentheses are required to parse this as an expression",
848-
vec![
849-
(stmt.span.shrink_to_lo(), "(".to_string()),
850-
(stmt.span.shrink_to_hi(), ")".to_string()),
851-
],
852-
Applicability::MachineApplicable,
853-
);
839+
err.subdiagnostic(ExprParenthesesNeeded::surrounding(stmt.span));
854840
}
855841
}
856842
}

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::ty::{
2222
impl IntoDiagArg for Ty<'_> {
2323
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
2424
ty::tls::with(|tcx| {
25-
let ty = tcx.short_string(self, path);
25+
let ty = tcx.short_string(tcx.lift(self), path);
2626
DiagArgValue::Str(std::borrow::Cow::Owned(ty))
2727
})
2828
}
@@ -31,7 +31,7 @@ impl IntoDiagArg for Ty<'_> {
3131
impl IntoDiagArg for Instance<'_> {
3232
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
3333
ty::tls::with(|tcx| {
34-
let instance = tcx.short_string_namespace(self, path, Namespace::ValueNS);
34+
let instance = tcx.short_string_namespace(tcx.lift(self), path, Namespace::ValueNS);
3535
DiagArgValue::Str(std::borrow::Cow::Owned(instance))
3636
})
3737
}

compiler/rustc_middle/src/ty/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_macros::extension;
1212
pub use rustc_type_ir::error::ExpectedFound;
1313

1414
use crate::ty::print::{FmtPrinter, Print, with_forced_trimmed_paths};
15-
use crate::ty::{self, Lift, Ty, TyCtxt};
15+
use crate::ty::{self, Ty, TyCtxt};
1616

1717
pub type TypeError<'tcx> = rustc_type_ir::error::TypeError<TyCtxt<'tcx>>;
1818

@@ -220,10 +220,10 @@ impl<'tcx> Ty<'tcx> {
220220
impl<'tcx> TyCtxt<'tcx> {
221221
pub fn string_with_limit<T>(self, t: T, length_limit: usize, ns: hir::def::Namespace) -> String
222222
where
223-
T: Copy + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
223+
T: Copy + for<'a> Print<FmtPrinter<'a, 'tcx>>,
224224
{
225225
let mut type_limit = 50;
226-
let regular = FmtPrinter::print_string(self, ns, |p| self.lift(t).print(p))
226+
let regular = FmtPrinter::print_string(self, ns, |p| t.print(p))
227227
.expect("could not write to `String`");
228228
if regular.len() <= length_limit {
229229
return regular;
@@ -233,7 +233,7 @@ impl<'tcx> TyCtxt<'tcx> {
233233
// Look for the longest properly trimmed path that still fits in length_limit.
234234
short = with_forced_trimmed_paths!({
235235
let mut p = FmtPrinter::new_with_limit(self, ns, Limit(type_limit));
236-
self.lift(t).print(&mut p).expect("could not print type");
236+
t.print(&mut p).expect("could not print type");
237237
p.into_buffer()
238238
});
239239
if short.len() <= length_limit || type_limit == 0 {
@@ -250,7 +250,7 @@ impl<'tcx> TyCtxt<'tcx> {
250250
/// where we wrote the file to is only printed once. The path will use the type namespace.
251251
pub fn short_string<T>(self, t: T, path: &mut Option<PathBuf>) -> String
252252
where
253-
T: Copy + Hash + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
253+
T: Copy + Hash + for<'a> Print<FmtPrinter<'a, 'tcx>>,
254254
{
255255
self.short_string_namespace(t, path, hir::def::Namespace::TypeNS)
256256
}
@@ -266,9 +266,9 @@ impl<'tcx> TyCtxt<'tcx> {
266266
namespace: hir::def::Namespace,
267267
) -> String
268268
where
269-
T: Copy + Hash + for<'a, 'b> Lift<TyCtxt<'b>, Lifted: Print<'b, FmtPrinter<'a, 'b>>>,
269+
T: Copy + Hash + for<'a> Print<FmtPrinter<'a, 'tcx>>,
270270
{
271-
let regular = FmtPrinter::print_string(self, namespace, |p| self.lift(t).print(p))
271+
let regular = FmtPrinter::print_string(self, namespace, |p| t.print(p))
272272
.expect("could not write to `String`");
273273

274274
if !self.sess.opts.unstable_opts.write_long_types_to_disk || self.sess.opts.verbose {

compiler/rustc_middle/src/ty/predicate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'tcx> Predicate<'tcx> {
113113
impl<'tcx> rustc_errors::IntoDiagArg for Predicate<'tcx> {
114114
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
115115
ty::tls::with(|tcx| {
116-
let pred = tcx.short_string(self, path);
116+
let pred = tcx.short_string(tcx.lift(self), path);
117117
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(pred))
118118
})
119119
}
@@ -122,7 +122,7 @@ impl<'tcx> rustc_errors::IntoDiagArg for Predicate<'tcx> {
122122
impl<'tcx> rustc_errors::IntoDiagArg for Clause<'tcx> {
123123
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
124124
ty::tls::with(|tcx| {
125-
let clause = tcx.short_string(self, path);
125+
let clause = tcx.short_string(tcx.lift(self), path);
126126
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(clause))
127127
})
128128
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::Lift;
1515

1616
pub type PrintError = std::fmt::Error;
1717

18-
pub trait Print<'tcx, P> {
18+
pub trait Print<P> {
1919
fn print(&self, p: &mut P) -> Result<(), PrintError>;
2020
}
2121

@@ -350,19 +350,19 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
350350
characteristic_def_id_of_type_cached(ty, &mut SsoHashSet::new())
351351
}
352352

353-
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
353+
impl<'tcx, P: Printer<'tcx>> Print<P> for ty::Region<'tcx> {
354354
fn print(&self, p: &mut P) -> Result<(), PrintError> {
355355
p.print_region(*self)
356356
}
357357
}
358358

359-
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
359+
impl<'tcx, P: Printer<'tcx>> Print<P> for Ty<'tcx> {
360360
fn print(&self, p: &mut P) -> Result<(), PrintError> {
361361
p.print_type(*self)
362362
}
363363
}
364364

365-
impl<'tcx, P: Printer<'tcx> + std::fmt::Write> Print<'tcx, P> for ty::Instance<'tcx> {
365+
impl<'tcx, P: Printer<'tcx> + std::fmt::Write> Print<P> for ty::Instance<'tcx> {
366366
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
367367
cx.print_def_path(self.def_id(), self.args)?;
368368
match self.def {
@@ -399,21 +399,21 @@ impl<'tcx, P: Printer<'tcx> + std::fmt::Write> Print<'tcx, P> for ty::Instance<'
399399
}
400400
}
401401

402-
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
402+
impl<'tcx, P: Printer<'tcx>> Print<P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
403403
fn print(&self, p: &mut P) -> Result<(), PrintError> {
404404
p.print_dyn_existential(self)
405405
}
406406
}
407407

408-
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
408+
impl<'tcx, P: Printer<'tcx>> Print<P> for ty::Const<'tcx> {
409409
fn print(&self, p: &mut P) -> Result<(), PrintError> {
410410
p.print_const(*self)
411411
}
412412
}
413413

414414
impl<T> rustc_type_ir::ir_print::IrPrint<T> for TyCtxt<'_>
415415
where
416-
T: Copy + for<'a, 'tcx> Lift<TyCtxt<'tcx>, Lifted: Print<'tcx, FmtPrinter<'a, 'tcx>>>,
416+
T: Copy + for<'a, 'tcx> Lift<TyCtxt<'tcx>, Lifted: Print<FmtPrinter<'a, 'tcx>>>,
417417
{
418418
fn print(t: &T, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
419419
ty::tls::with(|tcx| {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
265265

266266
fn pretty_print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
267267
where
268-
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
268+
T: Print<Self> + TypeFoldable<TyCtxt<'tcx>>,
269269
{
270270
value.as_ref().skip_binder().print(self)
271271
}
@@ -285,7 +285,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
285285
/// Prints comma-separated elements.
286286
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
287287
where
288-
T: Print<'tcx, Self>,
288+
T: Print<Self>,
289289
{
290290
if let Some(first) = elems.next() {
291291
first.print(self)?;
@@ -2484,7 +2484,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
24842484

24852485
fn pretty_print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
24862486
where
2487-
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
2487+
T: Print<Self> + TypeFoldable<TyCtxt<'tcx>>,
24882488
{
24892489
self.wrap_binder(value, WrapBinderMode::ForAll, |new_value, this| new_value.print(this))
24902490
}
@@ -2940,18 +2940,18 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
29402940
}
29412941
}
29422942

2943-
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T>
2943+
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<P> for ty::Binder<'tcx, T>
29442944
where
2945-
T: Print<'tcx, P> + TypeFoldable<TyCtxt<'tcx>>,
2945+
T: Print<P> + TypeFoldable<TyCtxt<'tcx>>,
29462946
{
29472947
fn print(&self, p: &mut P) -> Result<(), PrintError> {
29482948
p.pretty_print_in_binder(self)
29492949
}
29502950
}
29512951

2952-
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<'tcx, T>
2952+
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<P> for ty::OutlivesPredicate<'tcx, T>
29532953
where
2954-
T: Print<'tcx, P>,
2954+
T: Print<P>,
29552955
{
29562956
fn print(&self, p: &mut P) -> Result<(), PrintError> {
29572957
self.0.print(p)?;
@@ -2970,7 +2970,7 @@ pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);
29702970
impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintOnlyTraitPath<'tcx> {
29712971
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
29722972
ty::tls::with(|tcx| {
2973-
let trait_ref = tcx.short_string(self, path);
2973+
let trait_ref = tcx.short_string(tcx.lift(self), path);
29742974
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
29752975
})
29762976
}
@@ -2990,7 +2990,7 @@ pub struct TraitRefPrintSugared<'tcx>(ty::TraitRef<'tcx>);
29902990
impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintSugared<'tcx> {
29912991
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
29922992
ty::tls::with(|tcx| {
2993-
let trait_ref = tcx.short_string(self, path);
2993+
let trait_ref = tcx.short_string(tcx.lift(self), path);
29942994
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
29952995
})
29962996
}
@@ -3108,7 +3108,7 @@ macro_rules! forward_display_to_print {
31083108

31093109
macro_rules! define_print {
31103110
(($self:ident, $p:ident): $($ty:ty $print:block)+) => {
3111-
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
3111+
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<P> for $ty {
31123112
fn print(&$self, $p: &mut P) -> Result<(), PrintError> {
31133113
let _: () = $print;
31143114
Ok(())

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'tcx> PrettyPrinter<'tcx> for LegacySymbolMangler<'tcx> {
480480
// Identical to `PrettyPrinter::comma_sep` except there is no space after each comma.
481481
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
482482
where
483-
T: Print<'tcx, Self>,
483+
T: Print<Self>,
484484
{
485485
if let Some(first) = elems.next() {
486486
first.print(self)?;

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) struct Highlighted<'tcx, T> {
2828

2929
impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T>
3030
where
31-
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
31+
T: for<'a> Print<FmtPrinter<'a, 'tcx>>,
3232
{
3333
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
3434
rustc_errors::DiagArgValue::Str(self.to_string().into())
@@ -43,7 +43,7 @@ impl<'tcx, T> Highlighted<'tcx, T> {
4343

4444
impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
4545
where
46-
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
46+
T: for<'a> Print<FmtPrinter<'a, 'tcx>>,
4747
{
4848
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4949
let mut p = ty::print::FmtPrinter::new(self.tcx, self.ns);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
6060
) -> Diag<'a> {
6161
fn with_short_path<'tcx, T>(tcx: TyCtxt<'tcx>, value: T) -> String
6262
where
63-
T: fmt::Display + Print<'tcx, FmtPrinter<'tcx, 'tcx>>,
63+
T: fmt::Display + for<'b> Print<FmtPrinter<'b, 'tcx>>,
6464
{
6565
let s = value.to_string();
6666
if s.len() > 50 {

0 commit comments

Comments
 (0)