Skip to content

Commit b33fc4e

Browse files
committed
Auto merge of rust-lang#126912 - matthiaskrgr:rollup-z5klm95, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - rust-lang#124460 (Show notice about "never used" of Debug for enum) - rust-lang#125740 (transmute size check: properly account for alignment) - rust-lang#126413 (compiletest: make the crash test error message abit more informative) - rust-lang#126618 (Mark assoc tys live only if the corresponding trait is live) - rust-lang#126673 (Ensure we don't accidentally succeed when we want to report an error) - rust-lang#126804 (On short error format, append primary span label to message) - rust-lang#126868 (not use offset when there is not ends with brace) - rust-lang#126893 (Eliminate the distinction between PREC_POSTFIX and PREC_PAREN precedence level) - rust-lang#126899 (Suggest inline const blocks for array initialization) - rust-lang#126909 (add `@kobzol` to bootstrap team for triagebot) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5a3e2a4 + 0659ab6 commit b33fc4e

Some content is hidden

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

42 files changed

+380
-192
lines changed

compiler/rustc_ast/src/util/parser.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ pub const PREC_JUMP: i8 = -30;
233233
pub const PREC_RANGE: i8 = -10;
234234
// The range 2..=14 is reserved for AssocOp binary operator precedences.
235235
pub const PREC_PREFIX: i8 = 50;
236-
pub const PREC_POSTFIX: i8 = 60;
237-
pub const PREC_PAREN: i8 = 99;
236+
pub const PREC_UNAMBIGUOUS: i8 = 60;
238237
pub const PREC_FORCE_PAREN: i8 = 100;
239238

240239
#[derive(Debug, Clone, Copy)]
@@ -325,37 +324,35 @@ impl ExprPrecedence {
325324
| ExprPrecedence::Let
326325
| ExprPrecedence::Unary => PREC_PREFIX,
327326

328-
// Unary, postfix
329-
ExprPrecedence::Await
327+
// Never need parens
328+
ExprPrecedence::Array
329+
| ExprPrecedence::Await
330+
| ExprPrecedence::Block
330331
| ExprPrecedence::Call
331-
| ExprPrecedence::MethodCall
332+
| ExprPrecedence::ConstBlock
332333
| ExprPrecedence::Field
334+
| ExprPrecedence::ForLoop
335+
| ExprPrecedence::FormatArgs
336+
| ExprPrecedence::Gen
337+
| ExprPrecedence::If
333338
| ExprPrecedence::Index
334-
| ExprPrecedence::Try
335339
| ExprPrecedence::InlineAsm
340+
| ExprPrecedence::Lit
341+
| ExprPrecedence::Loop
336342
| ExprPrecedence::Mac
337-
| ExprPrecedence::FormatArgs
343+
| ExprPrecedence::Match
344+
| ExprPrecedence::MethodCall
338345
| ExprPrecedence::OffsetOf
339-
| ExprPrecedence::PostfixMatch => PREC_POSTFIX,
340-
341-
// Never need parens
342-
ExprPrecedence::Array
346+
| ExprPrecedence::Paren
347+
| ExprPrecedence::Path
348+
| ExprPrecedence::PostfixMatch
343349
| ExprPrecedence::Repeat
350+
| ExprPrecedence::Struct
351+
| ExprPrecedence::Try
352+
| ExprPrecedence::TryBlock
344353
| ExprPrecedence::Tup
345-
| ExprPrecedence::Lit
346-
| ExprPrecedence::Path
347-
| ExprPrecedence::Paren
348-
| ExprPrecedence::If
349354
| ExprPrecedence::While
350-
| ExprPrecedence::ForLoop
351-
| ExprPrecedence::Loop
352-
| ExprPrecedence::Match
353-
| ExprPrecedence::ConstBlock
354-
| ExprPrecedence::Block
355-
| ExprPrecedence::TryBlock
356-
| ExprPrecedence::Gen
357-
| ExprPrecedence::Struct
358-
| ExprPrecedence::Err => PREC_PAREN,
355+
| ExprPrecedence::Err => PREC_UNAMBIGUOUS,
359356
}
360357
}
361358
}

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'a> State<'a> {
217217
fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
218218
let prec = match func.kind {
219219
ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
220-
_ => parser::PREC_POSTFIX,
220+
_ => parser::PREC_UNAMBIGUOUS,
221221
};
222222

223223
// Independent of parenthesization related to precedence, we must
@@ -257,7 +257,7 @@ impl<'a> State<'a> {
257257
// boundaries, `$receiver.method()` can be parsed back as a statement
258258
// containing an expression if and only if `$receiver` can be parsed as
259259
// a statement containing an expression.
260-
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX, fixup);
260+
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup);
261261

262262
self.word(".");
263263
self.print_ident(segment.ident);
@@ -489,7 +489,7 @@ impl<'a> State<'a> {
489489
self.space();
490490
}
491491
MatchKind::Postfix => {
492-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
492+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
493493
self.word_nbsp(".match");
494494
}
495495
}
@@ -549,7 +549,7 @@ impl<'a> State<'a> {
549549
self.print_block_with_attrs(blk, attrs);
550550
}
551551
ast::ExprKind::Await(expr, _) => {
552-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
552+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
553553
self.word(".await");
554554
}
555555
ast::ExprKind::Assign(lhs, rhs, _) => {
@@ -568,14 +568,14 @@ impl<'a> State<'a> {
568568
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
569569
}
570570
ast::ExprKind::Field(expr, ident) => {
571-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
571+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
572572
self.word(".");
573573
self.print_ident(*ident);
574574
}
575575
ast::ExprKind::Index(expr, index, _) => {
576576
self.print_expr_maybe_paren(
577577
expr,
578-
parser::PREC_POSTFIX,
578+
parser::PREC_UNAMBIGUOUS,
579579
fixup.leftmost_subexpression(),
580580
);
581581
self.word("[");
@@ -713,7 +713,7 @@ impl<'a> State<'a> {
713713
}
714714
}
715715
ast::ExprKind::Try(e) => {
716-
self.print_expr_maybe_paren(e, parser::PREC_POSTFIX, fixup);
716+
self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup);
717717
self.word("?")
718718
}
719719
ast::ExprKind::TryBlock(blk) => {

compiler/rustc_errors/src/emitter.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1353,10 +1353,11 @@ impl HumanEmitter {
13531353
buffer.append(0, ": ", header_style);
13541354
label_width += 2;
13551355
}
1356+
let mut line = 0;
13561357
for (text, _) in msgs.iter() {
13571358
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13581359
// Account for newlines to align output to its label.
1359-
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
1360+
for text in normalize_whitespace(&text).lines() {
13601361
buffer.append(
13611362
line,
13621363
&format!(
@@ -1366,6 +1367,25 @@ impl HumanEmitter {
13661367
),
13671368
header_style,
13681369
);
1370+
line += 1;
1371+
}
1372+
}
1373+
if self.short_message {
1374+
let labels = msp
1375+
.span_labels()
1376+
.into_iter()
1377+
.filter_map(|label| match label.label {
1378+
Some(msg) if label.is_primary => {
1379+
let text = self.translate_message(&msg, args).ok()?;
1380+
if !text.trim().is_empty() { Some(text.to_string()) } else { None }
1381+
}
1382+
_ => None,
1383+
})
1384+
.collect::<Vec<_>>()
1385+
.join(", ");
1386+
if !labels.is_empty() {
1387+
buffer.append(line, ": ", Style::NoStyle);
1388+
buffer.append(line, &labels, Style::NoStyle);
13691389
}
13701390
}
13711391
}

compiler/rustc_hir_analysis/src/check/mod.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,18 @@ fn missing_items_err(
211211
.collect::<Vec<_>>()
212212
.join("`, `");
213213

214-
// `Span` before impl block closing brace.
215-
let hi = full_impl_span.hi() - BytePos(1);
216-
// Point at the place right before the closing brace of the relevant `impl` to suggest
217-
// adding the associated item at the end of its body.
218-
let sugg_sp = full_impl_span.with_lo(hi).with_hi(hi);
214+
let sugg_sp = if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(full_impl_span)
215+
&& snippet.ends_with("}")
216+
{
217+
// `Span` before impl block closing brace.
218+
let hi = full_impl_span.hi() - BytePos(1);
219+
// Point at the place right before the closing brace of the relevant `impl` to suggest
220+
// adding the associated item at the end of its body.
221+
full_impl_span.with_lo(hi).with_hi(hi)
222+
} else {
223+
full_impl_span.shrink_to_hi()
224+
};
225+
219226
// Obtain the level of indentation ending in `sugg_sp`.
220227
let padding =
221228
tcx.sess.source_map().indentation_before(sugg_sp).unwrap_or_else(|| String::new());

compiler/rustc_hir_pretty/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ impl<'a> State<'a> {
11201120
fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
11211121
let prec = match func.kind {
11221122
hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
1123-
_ => parser::PREC_POSTFIX,
1123+
_ => parser::PREC_UNAMBIGUOUS,
11241124
};
11251125

11261126
self.print_expr_maybe_paren(func, prec);
@@ -1134,7 +1134,7 @@ impl<'a> State<'a> {
11341134
args: &[hir::Expr<'_>],
11351135
) {
11361136
let base_args = args;
1137-
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX);
1137+
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS);
11381138
self.word(".");
11391139
self.print_ident(segment.ident);
11401140

@@ -1478,12 +1478,12 @@ impl<'a> State<'a> {
14781478
self.print_expr_maybe_paren(rhs, prec);
14791479
}
14801480
hir::ExprKind::Field(expr, ident) => {
1481-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1481+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
14821482
self.word(".");
14831483
self.print_ident(ident);
14841484
}
14851485
hir::ExprKind::Index(expr, index, _) => {
1486-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1486+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
14871487
self.word("[");
14881488
self.print_expr(index);
14891489
self.word("]");

compiler/rustc_hir_typeck/src/callee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::method::MethodCallee;
33
use super::{Expectation, FnCtxt, TupleArgumentsFlag};
44

55
use crate::errors;
6-
use rustc_ast::util::parser::PREC_POSTFIX;
6+
use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
77
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
88
use rustc_hir::def::{self, CtorKind, Namespace, Res};
99
use rustc_hir::def_id::DefId;
@@ -656,7 +656,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
656656
};
657657

658658
if let Ok(rest_snippet) = rest_snippet {
659-
let sugg = if callee_expr.precedence().order() >= PREC_POSTFIX {
659+
let sugg = if callee_expr.precedence().order() >= PREC_UNAMBIGUOUS {
660660
vec![
661661
(up_to_rcvr_span, "".to_string()),
662662
(rest_span, format!(".{}({rest_snippet}", segment.ident)),

compiler/rustc_hir_typeck/src/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
946946

947947
fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
948948
let expr_prec = self.expr.precedence().order();
949-
let needs_parens = expr_prec < rustc_ast::util::parser::PREC_POSTFIX;
949+
let needs_parens = expr_prec < rustc_ast::util::parser::PREC_UNAMBIGUOUS;
950950

951951
let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
952952
let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);

compiler/rustc_hir_typeck/src/coercion.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1245,11 +1245,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12451245
expr,
12461246
);
12471247

1248-
return self
1248+
return Err(self
12491249
.commit_if_ok(|_| {
1250-
self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty)
1250+
self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty)
12511251
})
1252-
.map(|ok| self.register_infer_ok_obligations(ok));
1252+
.unwrap_err());
12531253
}
12541254
}
12551255

@@ -1259,10 +1259,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12591259
if let Some(e) = first_error {
12601260
Err(e)
12611261
} else {
1262-
self.commit_if_ok(|_| {
1263-
self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty)
1264-
})
1265-
.map(|ok| self.register_infer_ok_obligations(ok))
1262+
Err(self
1263+
.commit_if_ok(|_| {
1264+
self.at(cause, self.param_env).lub(
1265+
DefineOpaqueTypes::Yes,
1266+
prev_ty,
1267+
new_ty,
1268+
)
1269+
})
1270+
.unwrap_err())
12661271
}
12671272
}
12681273
Ok(ok) => {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
99
use core::cmp::min;
1010
use core::iter;
1111
use hir::def_id::LocalDefId;
12-
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
12+
use rustc_ast::util::parser::{ExprPrecedence, PREC_UNAMBIGUOUS};
1313
use rustc_data_structures::packed::Pu128;
1414
use rustc_errors::{Applicability, Diag, MultiSpan};
1515
use rustc_hir as hir;
@@ -1329,7 +1329,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13291329
{
13301330
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
13311331

1332-
let mut sugg = if expr.precedence().order() >= PREC_POSTFIX {
1332+
let mut sugg = if expr.precedence().order() >= PREC_UNAMBIGUOUS {
13331333
vec![(span.shrink_to_hi(), ".into()".to_owned())]
13341334
} else {
13351335
vec![
@@ -2868,7 +2868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28682868
"change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`",
28692869
);
28702870

2871-
let close_paren = if expr.precedence().order() < PREC_POSTFIX {
2871+
let close_paren = if expr.precedence().order() < PREC_UNAMBIGUOUS {
28722872
sugg.push((expr.span.shrink_to_lo(), "(".to_string()));
28732873
")"
28742874
} else {
@@ -2893,7 +2893,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28932893
let len = src.trim_end_matches(&checked_ty.to_string()).len();
28942894
expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
28952895
},
2896-
if expr.precedence().order() < PREC_POSTFIX {
2896+
if expr.precedence().order() < PREC_UNAMBIGUOUS {
28972897
// Readd `)`
28982898
format!("{expected_ty})")
28992899
} else {

compiler/rustc_hir_typeck/src/intrinsicck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7373
// Special-case transmuting from `typeof(function)` and
7474
// `Option<typeof(function)>` to present a clearer error.
7575
let from = unpack_option_like(tcx, from);
76-
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to)
76+
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to, _)) = (from.kind(), sk_to)
7777
&& size_to == Pointer(dl.instruction_address_space).size(&tcx)
7878
{
7979
struct_span_code_err!(tcx.dcx(), span, E0591, "can't transmute zero-sized type")
@@ -88,7 +88,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8888
// Try to display a sensible error with as much information as possible.
8989
let skeleton_string = |ty: Ty<'tcx>, sk: Result<_, &_>| match sk {
9090
Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
91-
Ok(SizeSkeleton::Known(size)) => {
91+
Ok(SizeSkeleton::Known(size, _)) => {
9292
if let Some(v) = u128::from(size.bytes()).checked_mul(8) {
9393
format!("{v} bits")
9494
} else {

0 commit comments

Comments
 (0)