Skip to content

Commit e838059

Browse files
committed
Auto merge of #97632 - JohnTitor:rollup-d2ucrjw, r=JohnTitor
Rollup of 6 pull requests Successful merges: - #95594 (Additional `*mut [T]` methods) - #97130 (rustdoc: avoid including impl blocks with filled-in generics) - #97166 (Move conditions out of recover/report functions.) - #97605 (Mention filename in suggestion when it differs from primary span) - #97613 (rustdoc: Improve calculation of "Impls on Foreign Types") - #97626 (rename PointerAddress → PointerExposeAddress) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 12cd71f + b78c118 commit e838059

File tree

31 files changed

+293
-82
lines changed

31 files changed

+293
-82
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21472147
}
21482148
}
21492149

2150-
CastKind::PointerAddress => {
2150+
CastKind::PointerExposeAddress => {
21512151
let ty_from = op.ty(body, tcx);
21522152
let cast_ty_from = CastTy::from_ty(ty_from);
21532153
let cast_ty_to = CastTy::from_ty(*ty);

compiler/rustc_codegen_cranelift/src/base.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,11 @@ fn codegen_stmt<'tcx>(
607607
let operand = codegen_operand(fx, operand);
608608
lval.write_cvalue(fx, operand.cast_pointer_to(to_layout));
609609
}
610-
Rvalue::Cast(CastKind::Misc | CastKind::PointerAddress, ref operand, to_ty) => {
610+
Rvalue::Cast(
611+
CastKind::Misc | CastKind::PointerExposeAddress,
612+
ref operand,
613+
to_ty,
614+
) => {
611615
let operand = codegen_operand(fx, operand);
612616
let from_ty = operand.layout().ty;
613617
let to_ty = fx.monomorphize(to_ty);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
181181
let cast = bx.cx().layout_of(self.monomorphize(mir_cast_ty));
182182

183183
let val = match *kind {
184-
mir::CastKind::PointerAddress => {
184+
mir::CastKind::PointerExposeAddress => {
185185
assert!(bx.cx().is_backend_immediate(cast));
186186
let llptr = operand.immediate();
187187
let llcast_ty = bx.cx().immediate_backend_type(cast);

compiler/rustc_const_eval/src/interpret/cast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3131
self.unsize_into(src, cast_ty, dest)?;
3232
}
3333

34-
PointerAddress => {
34+
PointerExposeAddress => {
3535
let src = self.read_immediate(src)?;
36-
let res = self.pointer_address_cast(&src, cast_ty)?;
36+
let res = self.pointer_expose_address_cast(&src, cast_ty)?;
3737
self.write_immediate(res, dest)?;
3838
}
3939

@@ -184,7 +184,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
184184
Ok(self.cast_from_int_like(scalar, src.layout, cast_ty)?.into())
185185
}
186186

187-
pub fn pointer_address_cast(
187+
pub fn pointer_expose_address_cast(
188188
&mut self,
189189
src: &ImmTy<'tcx, M::PointerTag>,
190190
cast_ty: Ty<'tcx>,

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
542542
// in the type of any local, which also excludes casts).
543543
}
544544

545-
Rvalue::Cast(CastKind::PointerAddress, _, _) => {
545+
Rvalue::Cast(CastKind::PointerExposeAddress, _, _) => {
546546
self.check_op(ops::RawPtrToIntCast);
547547
}
548548

compiler/rustc_const_eval/src/transform/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'tcx> Validator<'_, 'tcx> {
502502
Rvalue::ThreadLocalRef(_) => return Err(Unpromotable),
503503

504504
// ptr-to-int casts are not possible in consts and thus not promotable
505-
Rvalue::Cast(CastKind::PointerAddress, _, _) => return Err(Unpromotable),
505+
Rvalue::Cast(CastKind::PointerExposeAddress, _, _) => return Err(Unpromotable),
506506

507507
// int-to-ptr casts are fine, they just use the integer value at pointer type.
508508
Rvalue::Cast(_, operand, _) => {

compiler/rustc_errors/src/emitter.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ impl EmitterWriter {
17151715

17161716
fn emit_suggestion_default(
17171717
&mut self,
1718+
span: &MultiSpan,
17181719
suggestion: &CodeSuggestion,
17191720
args: &FluentArgs<'_>,
17201721
level: &Level,
@@ -1766,6 +1767,30 @@ impl EmitterWriter {
17661767
None,
17671768
}
17681769

1770+
if let Some(span) = span.primary_span() {
1771+
// Compare the primary span of the diagnostic with the span of the suggestion
1772+
// being emitted. If they belong to the same file, we don't *need* to show the
1773+
// file name, saving in verbosity, but if it *isn't* we do need it, otherwise we're
1774+
// telling users to make a change but not clarifying *where*.
1775+
let loc = sm.lookup_char_pos(parts[0].span.lo());
1776+
if loc.file.name != sm.span_to_filename(span) && loc.file.name.is_real() {
1777+
buffer.puts(row_num - 1, 0, "--> ", Style::LineNumber);
1778+
buffer.append(
1779+
row_num - 1,
1780+
&format!(
1781+
"{}:{}:{}",
1782+
sm.filename_for_diagnostics(&loc.file.name),
1783+
sm.doctest_offset_line(&loc.file.name, loc.line),
1784+
loc.col.0 + 1,
1785+
),
1786+
Style::LineAndColumn,
1787+
);
1788+
for _ in 0..max_line_num_len {
1789+
buffer.prepend(row_num - 1, " ", Style::NoStyle);
1790+
}
1791+
row_num += 1;
1792+
}
1793+
}
17691794
let show_code_change = if has_deletion && !is_multiline {
17701795
DisplaySuggestion::Diff
17711796
} else if (parts.len() != 1 || parts[0].snippet.trim() != complete.trim())
@@ -1787,7 +1812,7 @@ impl EmitterWriter {
17871812
assert!(!file_lines.lines.is_empty() || parts[0].span.is_dummy());
17881813

17891814
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
1790-
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
1815+
draw_col_separator_no_space(&mut buffer, row_num - 1, max_line_num_len + 1);
17911816
let mut lines = complete.lines();
17921817
if lines.clone().next().is_none() {
17931818
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
@@ -2046,9 +2071,13 @@ impl EmitterWriter {
20462071
) {
20472072
panic!("failed to emit error: {}", e);
20482073
}
2049-
} else if let Err(e) =
2050-
self.emit_suggestion_default(sugg, args, &Level::Help, max_line_num_len)
2051-
{
2074+
} else if let Err(e) = self.emit_suggestion_default(
2075+
span,
2076+
sugg,
2077+
args,
2078+
&Level::Help,
2079+
max_line_num_len,
2080+
) {
20522081
panic!("failed to emit error: {}", e);
20532082
};
20542083
}

compiler/rustc_middle/src/mir/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2607,16 +2607,17 @@ static_assert_size!(Rvalue<'_>, 40);
26072607
impl<'tcx> Rvalue<'tcx> {
26082608
#[inline]
26092609
pub fn is_pointer_int_cast(&self) -> bool {
2610-
matches!(self, Rvalue::Cast(CastKind::PointerAddress, _, _))
2610+
matches!(self, Rvalue::Cast(CastKind::PointerExposeAddress, _, _))
26112611
}
26122612
}
26132613

26142614
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
26152615
pub enum CastKind {
26162616
Misc,
2617-
/// A pointer to address cast. A cast between a pointer and an integer type,
2618-
/// or between a function pointer and an integer type.
2619-
PointerAddress,
2617+
/// An exposing pointer to address cast. A cast between a pointer and an integer type, or
2618+
/// between a function pointer and an integer type.
2619+
/// See the docs on `expose_addr` for more details.
2620+
PointerExposeAddress,
26202621
Pointer(PointerCast),
26212622
}
26222623

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
194194
let cast_ty = CastTy::from_ty(expr.ty);
195195
let cast_kind = match (from_ty, cast_ty) {
196196
(Some(CastTy::Ptr(_) | CastTy::FnPtr), Some(CastTy::Int(_))) => {
197-
CastKind::PointerAddress
197+
CastKind::PointerExposeAddress
198198
}
199199
(_, _) => CastKind::Misc,
200200
};

compiler/rustc_parse/src/parser/diagnostics.rs

+11-31
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::pat::Expected;
2-
use super::ty::{AllowPlus, RecoverQuestionMark};
32
use super::{
4-
BlockMode, CommaRecoveryMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions,
5-
SemiColonMode, SeqSep, TokenExpectType, TokenType,
3+
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep,
4+
TokenExpectType, TokenType,
65
};
76

87
use crate::lexer::UnmatchedBrace;
@@ -1233,26 +1232,14 @@ impl<'a> Parser<'a> {
12331232
}
12341233
}
12351234

1236-
pub(super) fn maybe_report_ambiguous_plus(
1237-
&mut self,
1238-
allow_plus: AllowPlus,
1239-
impl_dyn_multi: bool,
1240-
ty: &Ty,
1241-
) {
1242-
if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi {
1235+
pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) {
1236+
if impl_dyn_multi {
12431237
self.sess.emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(&ty), span: ty.span });
12441238
}
12451239
}
12461240

12471241
/// Swift lets users write `Ty?` to mean `Option<Ty>`. Parse the construct and recover from it.
1248-
pub(super) fn maybe_recover_from_question_mark(
1249-
&mut self,
1250-
ty: P<Ty>,
1251-
recover_question_mark: RecoverQuestionMark,
1252-
) -> P<Ty> {
1253-
if let RecoverQuestionMark::No = recover_question_mark {
1254-
return ty;
1255-
}
1242+
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P<Ty>) -> P<Ty> {
12561243
if self.token == token::Question {
12571244
self.bump();
12581245
self.struct_span_err(self.prev_token.span, "invalid `?` in type")
@@ -1272,13 +1259,9 @@ impl<'a> Parser<'a> {
12721259
}
12731260
}
12741261

1275-
pub(super) fn maybe_recover_from_bad_type_plus(
1276-
&mut self,
1277-
allow_plus: AllowPlus,
1278-
ty: &Ty,
1279-
) -> PResult<'a, ()> {
1262+
pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
12801263
// Do not add `+` to expected tokens.
1281-
if matches!(allow_plus, AllowPlus::No) || !self.token.is_like_plus() {
1264+
if !self.token.is_like_plus() {
12821265
return Ok(());
12831266
}
12841267

@@ -1444,10 +1427,9 @@ impl<'a> Parser<'a> {
14441427
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
14451428
&mut self,
14461429
base: P<T>,
1447-
allow_recovery: bool,
14481430
) -> PResult<'a, P<T>> {
14491431
// Do not add `::` to expected tokens.
1450-
if allow_recovery && self.token == token::ModSep {
1432+
if self.token == token::ModSep {
14511433
if let Some(ty) = base.to_ty() {
14521434
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
14531435
}
@@ -1593,7 +1575,7 @@ impl<'a> Parser<'a> {
15931575
_ => ExprKind::Await(expr),
15941576
};
15951577
let expr = self.mk_expr(lo.to(sp), kind, attrs);
1596-
self.maybe_recover_from_bad_qpath(expr, true)
1578+
self.maybe_recover_from_bad_qpath(expr)
15971579
}
15981580

15991581
fn recover_await_macro(&mut self) -> PResult<'a, (Span, P<Expr>, bool)> {
@@ -2457,10 +2439,9 @@ impl<'a> Parser<'a> {
24572439
pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
24582440
&mut self,
24592441
mut first_pat: P<Pat>,
2460-
ra: RecoverColon,
24612442
expected: Expected,
24622443
) -> P<Pat> {
2463-
if RecoverColon::Yes != ra || token::Colon != self.token.kind {
2444+
if token::Colon != self.token.kind {
24642445
return first_pat;
24652446
}
24662447
if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
@@ -2594,10 +2575,9 @@ impl<'a> Parser<'a> {
25942575
pub(crate) fn maybe_recover_unexpected_comma(
25952576
&mut self,
25962577
lo: Span,
2597-
rc: RecoverComma,
25982578
rt: CommaRecoveryMode,
25992579
) -> PResult<'a, ()> {
2600-
if rc == RecoverComma::No || self.token != token::Comma {
2580+
if self.token != token::Comma {
26012581
return Ok(());
26022582
}
26032583

compiler/rustc_parse/src/parser/expr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ impl<'a> Parser<'a> {
14171417
match self.parse_opt_lit() {
14181418
Some(literal) => {
14191419
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(literal), attrs);
1420-
self.maybe_recover_from_bad_qpath(expr, true)
1420+
self.maybe_recover_from_bad_qpath(expr)
14211421
}
14221422
None => self.try_macro_suggestion(),
14231423
}
@@ -1444,7 +1444,7 @@ impl<'a> Parser<'a> {
14441444
ExprKind::Tup(es)
14451445
};
14461446
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1447-
self.maybe_recover_from_bad_qpath(expr, true)
1447+
self.maybe_recover_from_bad_qpath(expr)
14481448
}
14491449

14501450
fn parse_array_or_repeat_expr(
@@ -1481,7 +1481,7 @@ impl<'a> Parser<'a> {
14811481
}
14821482
};
14831483
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1484-
self.maybe_recover_from_bad_qpath(expr, true)
1484+
self.maybe_recover_from_bad_qpath(expr)
14851485
}
14861486

14871487
fn parse_path_start_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
@@ -1519,7 +1519,7 @@ impl<'a> Parser<'a> {
15191519
};
15201520

15211521
let expr = self.mk_expr(lo.to(hi), kind, attrs);
1522-
self.maybe_recover_from_bad_qpath(expr, true)
1522+
self.maybe_recover_from_bad_qpath(expr)
15231523
}
15241524

15251525
/// Parse `'label: $expr`. The label is already parsed.
@@ -1604,7 +1604,7 @@ impl<'a> Parser<'a> {
16041604
let lo = self.prev_token.span;
16051605
let kind = ExprKind::Ret(self.parse_expr_opt()?);
16061606
let expr = self.mk_expr(lo.to(self.prev_token.span), kind, attrs);
1607-
self.maybe_recover_from_bad_qpath(expr, true)
1607+
self.maybe_recover_from_bad_qpath(expr)
16081608
}
16091609

16101610
/// Parse `"do" "yeet" expr?`.
@@ -1619,7 +1619,7 @@ impl<'a> Parser<'a> {
16191619
let span = lo.to(self.prev_token.span);
16201620
self.sess.gated_spans.gate(sym::yeet_expr, span);
16211621
let expr = self.mk_expr(span, kind, attrs);
1622-
self.maybe_recover_from_bad_qpath(expr, true)
1622+
self.maybe_recover_from_bad_qpath(expr)
16231623
}
16241624

16251625
/// Parse `"break" (('label (:? expr)?) | expr?)` with `"break"` token already eaten.
@@ -1679,7 +1679,7 @@ impl<'a> Parser<'a> {
16791679
None
16801680
};
16811681
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Break(label, kind), attrs);
1682-
self.maybe_recover_from_bad_qpath(expr, true)
1682+
self.maybe_recover_from_bad_qpath(expr)
16831683
}
16841684

16851685
/// Parse `"yield" expr?`.
@@ -1689,7 +1689,7 @@ impl<'a> Parser<'a> {
16891689
let span = lo.to(self.prev_token.span);
16901690
self.sess.gated_spans.gate(sym::generators, span);
16911691
let expr = self.mk_expr(span, kind, attrs);
1692-
self.maybe_recover_from_bad_qpath(expr, true)
1692+
self.maybe_recover_from_bad_qpath(expr)
16931693
}
16941694

16951695
/// Returns a string literal if the next token is a string literal.

compiler/rustc_parse/src/parser/pat.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ impl<'a> Parser<'a> {
100100
};
101101

102102
// Parse the first pattern (`p_0`).
103-
let first_pat = self.parse_pat_no_top_alt(expected)?;
104-
self.maybe_recover_unexpected_comma(first_pat.span, rc, rt)?;
103+
let mut first_pat = self.parse_pat_no_top_alt(expected)?;
104+
if rc == RecoverComma::Yes {
105+
self.maybe_recover_unexpected_comma(first_pat.span, rt)?;
106+
}
105107

106108
// If the next token is not a `|`,
107109
// this is not an or-pattern and we should exit here.
@@ -111,7 +113,9 @@ impl<'a> Parser<'a> {
111113
// This complicated procedure is done purely for diagnostics UX.
112114

113115
// Check if the user wrote `foo:bar` instead of `foo::bar`.
114-
let first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, ra, expected);
116+
if ra == RecoverColon::Yes {
117+
first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, expected);
118+
}
115119

116120
if let Some(leading_vert_span) = leading_vert_span {
117121
// If there was a leading vert, treat this as an or-pattern. This improves
@@ -139,7 +143,9 @@ impl<'a> Parser<'a> {
139143
err.span_label(lo, WHILE_PARSING_OR_MSG);
140144
err
141145
})?;
142-
self.maybe_recover_unexpected_comma(pat.span, rc, rt)?;
146+
if rc == RecoverComma::Yes {
147+
self.maybe_recover_unexpected_comma(pat.span, rt)?;
148+
}
143149
pats.push(pat);
144150
}
145151
let or_pattern_span = lo.to(self.prev_token.span);
@@ -408,7 +414,7 @@ impl<'a> Parser<'a> {
408414
};
409415

410416
let pat = self.mk_pat(lo.to(self.prev_token.span), pat);
411-
let pat = self.maybe_recover_from_bad_qpath(pat, true)?;
417+
let pat = self.maybe_recover_from_bad_qpath(pat)?;
412418
let pat = self.recover_intersection_pat(pat)?;
413419

414420
if !allow_range_pat {

compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a> Parser<'a> {
180180
} else {
181181
// Since none of the above applied, this is an expression statement macro.
182182
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new());
183-
let e = self.maybe_recover_from_bad_qpath(e, true)?;
183+
let e = self.maybe_recover_from_bad_qpath(e)?;
184184
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
185185
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
186186
StmtKind::Expr(e)

0 commit comments

Comments
 (0)