Skip to content

Commit 1f7fb64

Browse files
committed
Auto merge of #95889 - Dylan-DPC:rollup-1cmywu4, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #95566 (Avoid duplication of doc comments in `std::char` constants and functions) - #95784 (Suggest replacing `typeof(...)` with an actual type) - #95807 (Suggest adding a local for vector to fix borrowck errors) - #95849 (Check for git submodules in non-git source tree.) - #95852 (Fix missing space in lossy provenance cast lint) - #95857 (Allow multiple derefs to be splitted in deref_separator) - #95868 (rustdoc: Reduce allocations in a `html::markdown` function) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 027a232 + fcfecab commit 1f7fb64

22 files changed

+333
-235
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+31-9
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,22 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
785785
issued_borrow: &BorrowData<'tcx>,
786786
explanation: BorrowExplanation,
787787
) {
788-
let used_in_call =
789-
matches!(explanation, BorrowExplanation::UsedLater(LaterUseKind::Call, _call_span, _));
788+
let used_in_call = matches!(
789+
explanation,
790+
BorrowExplanation::UsedLater(LaterUseKind::Call | LaterUseKind::Other, _call_span, _)
791+
);
790792
if !used_in_call {
791793
debug!("not later used in call");
792794
return;
793795
}
794796

797+
let use_span =
798+
if let BorrowExplanation::UsedLater(LaterUseKind::Other, use_span, _) = explanation {
799+
Some(use_span)
800+
} else {
801+
None
802+
};
803+
795804
let outer_call_loc =
796805
if let TwoPhaseActivation::ActivatedAt(loc) = issued_borrow.activation_location {
797806
loc
@@ -835,7 +844,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
835844
debug!("===> outer_call_loc = {:?}, inner_call_loc = {:?}", outer_call_loc, inner_call_loc);
836845

837846
let inner_call_span = inner_call_term.source_info.span;
838-
let outer_call_span = outer_call_stmt.either(|s| s.source_info, |t| t.source_info).span;
847+
let outer_call_span = match use_span {
848+
Some(span) => span,
849+
None => outer_call_stmt.either(|s| s.source_info, |t| t.source_info).span,
850+
};
839851
if outer_call_span == inner_call_span || !outer_call_span.contains(inner_call_span) {
840852
// FIXME: This stops the suggestion in some cases where it should be emitted.
841853
// Fix the spans for those cases so it's emitted correctly.
@@ -845,8 +857,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
845857
);
846858
return;
847859
}
848-
err.span_help(inner_call_span, "try adding a local storing this argument...");
849-
err.span_help(outer_call_span, "...and then using that local as the argument to this call");
860+
err.span_help(
861+
inner_call_span,
862+
&format!(
863+
"try adding a local storing this{}...",
864+
if use_span.is_some() { "" } else { " argument" }
865+
),
866+
);
867+
err.span_help(
868+
outer_call_span,
869+
&format!(
870+
"...and then using that local {}",
871+
if use_span.is_some() { "here" } else { "as the argument to this call" }
872+
),
873+
);
850874
}
851875

852876
fn suggest_split_at_mut_if_applicable(
@@ -1912,10 +1936,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19121936
} else {
19131937
"cannot assign twice to immutable variable"
19141938
};
1915-
if span != assigned_span {
1916-
if !from_arg {
1917-
err.span_label(assigned_span, format!("first assignment to {}", place_description));
1918-
}
1939+
if span != assigned_span && !from_arg {
1940+
err.span_label(assigned_span, format!("first assignment to {}", place_description));
19191941
}
19201942
if let Some(decl) = local_decl
19211943
&& let Some(name) = local_name

compiler/rustc_error_messages/locales/en-US/diagnostics.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typeck-functional-record-update-on-non-struct =
6262
6363
typeck-typeof-reserved-keyword-used =
6464
`typeof` is a reserved keyword but unimplemented
65+
.suggestion = consider replacing `typeof(...)` with an actual type
6566
.label = reserved keyword
6667
6768
typeck-return-stmt-outside-of-fn-body =

compiler/rustc_mir_transform/src/deref_separator.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1111
for (i, stmt) in data.statements.iter_mut().enumerate() {
1212
match stmt.kind {
1313
StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => {
14+
let mut place_local = place.local;
15+
let mut last_len = 0;
1416
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
1517
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
1618
// The type that we are derefing.
@@ -23,15 +25,18 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2325
patch.add_statement(loc, StatementKind::StorageLive(temp));
2426

2527
// We are adding current p_ref's projections to our
26-
// temp value.
27-
let deref_place =
28-
Place::from(p_ref.local).project_deeper(p_ref.projection, tcx);
28+
// temp value, excluding projections we already covered.
29+
let deref_place = Place::from(place_local)
30+
.project_deeper(&p_ref.projection[last_len..], tcx);
2931
patch.add_assign(
3032
loc,
3133
Place::from(temp),
3234
Rvalue::Use(Operand::Move(deref_place)),
3335
);
3436

37+
place_local = temp;
38+
last_len = p_ref.projection.len();
39+
3540
// We are creating a place by using our temp value's location
3641
// and copying derefed values which we need to create new statement.
3742
let temp_place =
@@ -50,11 +55,6 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5055
// Since our job with the temp is done it should be gone
5156
let loc = Location { block: block, statement_index: i + 1 };
5257
patch.add_statement(loc, StatementKind::StorageDead(temp));
53-
54-
// As all projections are off the base projection, if there are
55-
// multiple derefs in the middle of projection, it might cause
56-
// unsoundness, to not let that happen we break the loop.
57-
break;
5858
}
5959
}
6060
}

compiler/rustc_typeck/src/astconv/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2460,8 +2460,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24602460
self.normalize_ty(ast_ty.span, array_ty)
24612461
}
24622462
hir::TyKind::Typeof(ref e) => {
2463-
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
2464-
tcx.type_of(tcx.hir().local_def_id(e.hir_id))
2463+
let ty = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
2464+
let span = ast_ty.span;
2465+
tcx.sess.emit_err(TypeofReservedKeywordUsed {
2466+
span,
2467+
ty,
2468+
opt_sugg: Some((span, Applicability::MachineApplicable))
2469+
.filter(|_| ty.is_suggestable()),
2470+
});
2471+
2472+
ty
24652473
}
24662474
hir::TyKind::Infer => {
24672475
// Infer also appears as the type of arguments or return

compiler/rustc_typeck/src/check/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10121012
err.help(msg);
10131013
}
10141014
err.help(
1015-
"if you can't comply with strict provenance and need to expose the pointer\
1015+
"if you can't comply with strict provenance and need to expose the pointer \
10161016
provenance you can use `.expose_addr()` instead"
10171017
);
10181018

compiler/rustc_typeck/src/errors.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Errors emitted by typeck.
2+
use rustc_errors::Applicability;
23
use rustc_macros::SessionDiagnostic;
4+
use rustc_middle::ty::Ty;
35
use rustc_span::{symbol::Ident, Span, Symbol};
46

57
#[derive(SessionDiagnostic)]
@@ -127,10 +129,13 @@ pub struct FunctionalRecordUpdateOnNonStruct {
127129

128130
#[derive(SessionDiagnostic)]
129131
#[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
130-
pub struct TypeofReservedKeywordUsed {
132+
pub struct TypeofReservedKeywordUsed<'tcx> {
133+
pub ty: Ty<'tcx>,
131134
#[primary_span]
132135
#[label]
133136
pub span: Span,
137+
#[suggestion_verbose(message = "suggestion", code = "{ty}")]
138+
pub opt_sugg: Option<(Span, Applicability)>,
134139
}
135140

136141
#[derive(SessionDiagnostic)]

library/core/src/char/convert.rs

+7-132
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,22 @@ use crate::fmt;
66
use crate::mem::transmute;
77
use crate::str::FromStr;
88

9-
/// Converts a `u32` to a `char`.
10-
///
11-
/// Note that all [`char`]s are valid [`u32`]s, and can be cast to one with
12-
/// `as`:
13-
///
14-
/// ```
15-
/// let c = '💯';
16-
/// let i = c as u32;
17-
///
18-
/// assert_eq!(128175, i);
19-
/// ```
20-
///
21-
/// However, the reverse is not true: not all valid [`u32`]s are valid
22-
/// [`char`]s. `from_u32()` will return `None` if the input is not a valid value
23-
/// for a [`char`].
24-
///
25-
/// For an unsafe version of this function which ignores these checks, see
26-
/// [`from_u32_unchecked`].
27-
///
28-
/// # Examples
29-
///
30-
/// Basic usage:
31-
///
32-
/// ```
33-
/// use std::char;
34-
///
35-
/// let c = char::from_u32(0x2764);
36-
///
37-
/// assert_eq!(Some('❤'), c);
38-
/// ```
39-
///
40-
/// Returning `None` when the input is not a valid [`char`]:
41-
///
42-
/// ```
43-
/// use std::char;
44-
///
45-
/// let c = char::from_u32(0x110000);
46-
///
47-
/// assert_eq!(None, c);
48-
/// ```
49-
#[doc(alias = "chr")]
9+
/// Converts a `u32` to a `char`. See [`char::from_u32`].
5010
#[must_use]
5111
#[inline]
52-
#[stable(feature = "rust1", since = "1.0.0")]
53-
#[rustc_const_unstable(feature = "const_char_convert", issue = "89259")]
54-
pub const fn from_u32(i: u32) -> Option<char> {
12+
pub(super) const fn from_u32(i: u32) -> Option<char> {
5513
// FIXME: once Result::ok is const fn, use it here
5614
match char_try_from_u32(i) {
5715
Ok(c) => Some(c),
5816
Err(_) => None,
5917
}
6018
}
6119

62-
/// Converts a `u32` to a `char`, ignoring validity.
63-
///
64-
/// Note that all [`char`]s are valid [`u32`]s, and can be cast to one with
65-
/// `as`:
66-
///
67-
/// ```
68-
/// let c = '💯';
69-
/// let i = c as u32;
70-
///
71-
/// assert_eq!(128175, i);
72-
/// ```
73-
///
74-
/// However, the reverse is not true: not all valid [`u32`]s are valid
75-
/// [`char`]s. `from_u32_unchecked()` will ignore this, and blindly cast to
76-
/// [`char`], possibly creating an invalid one.
77-
///
78-
/// # Safety
79-
///
80-
/// This function is unsafe, as it may construct invalid `char` values.
81-
///
82-
/// For a safe version of this function, see the [`from_u32`] function.
83-
///
84-
/// # Examples
85-
///
86-
/// Basic usage:
87-
///
88-
/// ```
89-
/// use std::char;
90-
///
91-
/// let c = unsafe { char::from_u32_unchecked(0x2764) };
92-
///
93-
/// assert_eq!('❤', c);
94-
/// ```
20+
/// Converts a `u32` to a `char`, ignoring validity. See [`char::from_u32_unchecked`].
21+
#[rustc_const_unstable(feature = "const_char_convert", issue = "89259")]
9522
#[inline]
9623
#[must_use]
97-
#[stable(feature = "char_from_unchecked", since = "1.5.0")]
98-
#[rustc_const_unstable(feature = "const_char_convert", issue = "89259")]
99-
pub const unsafe fn from_u32_unchecked(i: u32) -> char {
24+
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
10025
// SAFETY: the caller must guarantee that `i` is a valid char value.
10126
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
10227
}
@@ -317,60 +242,10 @@ impl fmt::Display for CharTryFromError {
317242
}
318243
}
319244

320-
/// Converts a digit in the given radix to a `char`.
321-
///
322-
/// A 'radix' here is sometimes also called a 'base'. A radix of two
323-
/// indicates a binary number, a radix of ten, decimal, and a radix of
324-
/// sixteen, hexadecimal, to give some common values. Arbitrary
325-
/// radices are supported.
326-
///
327-
/// `from_digit()` will return `None` if the input is not a digit in
328-
/// the given radix.
329-
///
330-
/// # Panics
331-
///
332-
/// Panics if given a radix larger than 36.
333-
///
334-
/// # Examples
335-
///
336-
/// Basic usage:
337-
///
338-
/// ```
339-
/// use std::char;
340-
///
341-
/// let c = char::from_digit(4, 10);
342-
///
343-
/// assert_eq!(Some('4'), c);
344-
///
345-
/// // Decimal 11 is a single digit in base 16
346-
/// let c = char::from_digit(11, 16);
347-
///
348-
/// assert_eq!(Some('b'), c);
349-
/// ```
350-
///
351-
/// Returning `None` when the input is not a digit:
352-
///
353-
/// ```
354-
/// use std::char;
355-
///
356-
/// let c = char::from_digit(20, 10);
357-
///
358-
/// assert_eq!(None, c);
359-
/// ```
360-
///
361-
/// Passing a large radix, causing a panic:
362-
///
363-
/// ```should_panic
364-
/// use std::char;
365-
///
366-
/// // this panics
367-
/// let c = char::from_digit(1, 37);
368-
/// ```
245+
/// Converts a digit in the given radix to a `char`. See [`char::from_digit`].
369246
#[inline]
370247
#[must_use]
371-
#[stable(feature = "rust1", since = "1.0.0")]
372-
#[rustc_const_unstable(feature = "const_char_convert", issue = "89259")]
373-
pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
248+
pub(super) const fn from_digit(num: u32, radix: u32) -> Option<char> {
374249
if radix > 36 {
375250
panic!("from_digit: radix is too high (maximum 36)");
376251
}

library/core/src/char/decode.rs

+2-47
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,9 @@ pub struct DecodeUtf16Error {
3030
}
3131

3232
/// Creates an iterator over the UTF-16 encoded code points in `iter`,
33-
/// returning unpaired surrogates as `Err`s.
34-
///
35-
/// # Examples
36-
///
37-
/// Basic usage:
38-
///
39-
/// ```
40-
/// use std::char::decode_utf16;
41-
///
42-
/// // 𝄞mus<invalid>ic<invalid>
43-
/// let v = [
44-
/// 0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834,
45-
/// ];
46-
///
47-
/// assert_eq!(
48-
/// decode_utf16(v.iter().cloned())
49-
/// .map(|r| r.map_err(|e| e.unpaired_surrogate()))
50-
/// .collect::<Vec<_>>(),
51-
/// vec![
52-
/// Ok('𝄞'),
53-
/// Ok('m'), Ok('u'), Ok('s'),
54-
/// Err(0xDD1E),
55-
/// Ok('i'), Ok('c'),
56-
/// Err(0xD834)
57-
/// ]
58-
/// );
59-
/// ```
60-
///
61-
/// A lossy decoder can be obtained by replacing `Err` results with the replacement character:
62-
///
63-
/// ```
64-
/// use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
65-
///
66-
/// // 𝄞mus<invalid>ic<invalid>
67-
/// let v = [
68-
/// 0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834,
69-
/// ];
70-
///
71-
/// assert_eq!(
72-
/// decode_utf16(v.iter().cloned())
73-
/// .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
74-
/// .collect::<String>(),
75-
/// "𝄞mus�ic�"
76-
/// );
77-
/// ```
78-
#[stable(feature = "decode_utf16", since = "1.9.0")]
33+
/// returning unpaired surrogates as `Err`s. See [`char::decode_utf16`].
7934
#[inline]
80-
pub fn decode_utf16<I: IntoIterator<Item = u16>>(iter: I) -> DecodeUtf16<I::IntoIter> {
35+
pub(super) fn decode_utf16<I: IntoIterator<Item = u16>>(iter: I) -> DecodeUtf16<I::IntoIter> {
8136
DecodeUtf16 { iter: iter.into_iter(), buf: None }
8237
}
8338

0 commit comments

Comments
 (0)