Skip to content

Commit cd12888

Browse files
committed
Auto merge of #104325 - GuillaumeGomez:rollup-19bzwoa, r=GuillaumeGomez
Rollup of 8 pull requests Successful merges: - #104110 (prevent uninitialized access in black_box for zero-sized-types) - #104117 (Mark `trait_upcasting` feature no longer incomplete.) - #104144 (Suggest removing unnecessary `.` to use a floating point literal) - #104250 (Migrate no result page link color to CSS variables) - #104261 (More accurately report error when formal and expected signature types differ) - #104263 (Add a reference to ilog2 in leading_zeros integer docs) - #104308 (Remove the old `ValidAlign` name) - #104319 (Fix non clickable source link) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8ef2485 + d532d67 commit cd12888

Some content is hidden

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

44 files changed

+377
-133
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,26 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
340340

341341
sym::black_box => {
342342
args[0].val.store(self, result);
343-
343+
let result_val_span = [result.llval];
344344
// We need to "use" the argument in some way LLVM can't introspect, and on
345345
// targets that support it we can typically leverage inline assembly to do
346346
// this. LLVM's interpretation of inline assembly is that it's, well, a black
347347
// box. This isn't the greatest implementation since it probably deoptimizes
348348
// more than we want, but it's so far good enough.
349+
//
350+
// For zero-sized types, the location pointed to by the result may be
351+
// uninitialized. Do not "use" the result in this case; instead just clobber
352+
// the memory.
353+
let (constraint, inputs): (&str, &[_]) = if result.layout.is_zst() {
354+
("~{memory}", &[])
355+
} else {
356+
("r,~{memory}", &result_val_span)
357+
};
349358
crate::asm::inline_asm_call(
350359
self,
351360
"",
352-
"r,~{memory}",
353-
&[result.llval],
361+
constraint,
362+
inputs,
354363
self.type_void(),
355364
true,
356365
false,

compiler/rustc_feature/src/active.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ declare_features! (
512512
(active, thread_local, "1.0.0", Some(29594), None),
513513
/// Allows defining `trait X = A + B;` alias items.
514514
(active, trait_alias, "1.24.0", Some(41517), None),
515-
/// Allows upcasting trait objects via supertraits.
516-
/// Trait upcasting is casting, e.g., `dyn Foo -> dyn Bar` where `Foo: Bar`.
517-
(incomplete, trait_upcasting, "1.56.0", Some(65991), None),
515+
/// Allows dyn upcasting trait objects via supertraits.
516+
/// Dyn upcasting is casting, e.g., `dyn Foo -> dyn Bar` where `Foo: Bar`.
517+
(active, trait_upcasting, "1.56.0", Some(65991), None),
518518
/// Allows #[repr(transparent)] on unions (RFC 2645).
519519
(active, transparent_unions, "1.37.0", Some(60405), None),
520520
/// Allows inconsistent bounds in where clauses.

compiler/rustc_hir_typeck/src/demand.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3030
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
3131
error: Option<TypeError<'tcx>>,
3232
) {
33+
if expr_ty == expected {
34+
return;
35+
}
36+
3337
self.annotate_expected_due_to_let_ty(err, expr, error);
3438

3539
// Use `||` to give these suggestions a precedence
@@ -43,7 +47,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4347
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
4448
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
4549
|| self.suggest_into(err, expr, expr_ty, expected)
46-
|| self.suggest_option_to_bool(err, expr, expr_ty, expected);
50+
|| self.suggest_option_to_bool(err, expr, expr_ty, expected)
51+
|| self.suggest_floating_point_literal(err, expr, expected);
4752

4853
self.note_type_is_not_clone(err, expected, expr_ty, expr);
4954
self.note_need_for_fn_pointer(err, expected, expr_ty);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
597597
}
598598
};
599599

600+
let mk_trace = |span, (formal_ty, expected_ty), provided_ty| {
601+
let mismatched_ty = if expected_ty == provided_ty {
602+
// If expected == provided, then we must have failed to sup
603+
// the formal type. Avoid printing out "expected Ty, found Ty"
604+
// in that case.
605+
formal_ty
606+
} else {
607+
expected_ty
608+
};
609+
TypeTrace::types(&self.misc(span), true, mismatched_ty, provided_ty)
610+
};
611+
600612
// The algorithm here is inspired by levenshtein distance and longest common subsequence.
601613
// We'll try to detect 4 different types of mistakes:
602614
// - An extra parameter has been provided that doesn't satisfy *any* of the other inputs
@@ -661,10 +673,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
661673
// A tuple wrap suggestion actually occurs within,
662674
// so don't do anything special here.
663675
err = self.err_ctxt().report_and_explain_type_error(
664-
TypeTrace::types(
665-
&self.misc(*lo),
666-
true,
667-
formal_and_expected_inputs[mismatch_idx.into()].1,
676+
mk_trace(
677+
*lo,
678+
formal_and_expected_inputs[mismatch_idx.into()],
668679
provided_arg_tys[mismatch_idx.into()].0,
669680
),
670681
terr,
@@ -748,9 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
748759
errors.drain_filter(|error| {
749760
let Error::Invalid(provided_idx, expected_idx, Compatibility::Incompatible(Some(e))) = error else { return false };
750761
let (provided_ty, provided_span) = provided_arg_tys[*provided_idx];
751-
let (expected_ty, _) = formal_and_expected_inputs[*expected_idx];
752-
let cause = &self.misc(provided_span);
753-
let trace = TypeTrace::types(cause, true, expected_ty, provided_ty);
762+
let trace = mk_trace(provided_span, formal_and_expected_inputs[*expected_idx], provided_ty);
754763
if !matches!(trace.cause.as_failure_code(*e), FailureCode::Error0308(_)) {
755764
self.err_ctxt().report_and_explain_type_error(trace, *e).emit();
756765
return true;
@@ -774,8 +783,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
774783
{
775784
let (formal_ty, expected_ty) = formal_and_expected_inputs[*expected_idx];
776785
let (provided_ty, provided_arg_span) = provided_arg_tys[*provided_idx];
777-
let cause = &self.misc(provided_arg_span);
778-
let trace = TypeTrace::types(cause, true, expected_ty, provided_ty);
786+
let trace = mk_trace(provided_arg_span, (formal_ty, expected_ty), provided_ty);
779787
let mut err = self.err_ctxt().report_and_explain_type_error(trace, *err);
780788
self.emit_coerce_suggestions(
781789
&mut err,
@@ -847,8 +855,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
847855
let (formal_ty, expected_ty) = formal_and_expected_inputs[expected_idx];
848856
let (provided_ty, provided_span) = provided_arg_tys[provided_idx];
849857
if let Compatibility::Incompatible(error) = compatibility {
850-
let cause = &self.misc(provided_span);
851-
let trace = TypeTrace::types(cause, true, expected_ty, provided_ty);
858+
let trace = mk_trace(provided_span, (formal_ty, expected_ty), provided_ty);
852859
if let Some(e) = error {
853860
self.err_ctxt().note_type_err(
854861
&mut err,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374374
let annotation_span = ty.span;
375375
err.span_suggestion(
376376
annotation_span.with_hi(annotation_span.lo()),
377-
format!("alternatively, consider changing the type annotation"),
377+
"alternatively, consider changing the type annotation",
378378
suggest_annotation,
379379
Applicability::MaybeIncorrect,
380380
);
@@ -1204,6 +1204,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12041204
}
12051205
}
12061206

1207+
#[instrument(skip(self, err))]
1208+
pub(crate) fn suggest_floating_point_literal(
1209+
&self,
1210+
err: &mut Diagnostic,
1211+
expr: &hir::Expr<'_>,
1212+
expected_ty: Ty<'tcx>,
1213+
) -> bool {
1214+
if !expected_ty.is_floating_point() {
1215+
return false;
1216+
}
1217+
match expr.kind {
1218+
ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [start, end], _) => {
1219+
err.span_suggestion_verbose(
1220+
start.span.shrink_to_hi().with_hi(end.span.lo()),
1221+
"remove the unnecessary `.` operator for a floating point literal",
1222+
'.',
1223+
Applicability::MaybeIncorrect,
1224+
);
1225+
true
1226+
}
1227+
ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, ..), [start], _) => {
1228+
err.span_suggestion_verbose(
1229+
expr.span.with_lo(start.span.hi()),
1230+
"remove the unnecessary `.` operator for a floating point literal",
1231+
'.',
1232+
Applicability::MaybeIncorrect,
1233+
);
1234+
true
1235+
}
1236+
ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, ..), [end], _) => {
1237+
err.span_suggestion_verbose(
1238+
expr.span.until(end.span),
1239+
"remove the unnecessary `.` operator and add an integer part for a floating point literal",
1240+
"0.",
1241+
Applicability::MaybeIncorrect,
1242+
);
1243+
true
1244+
}
1245+
_ => false,
1246+
}
1247+
}
1248+
12071249
fn is_loop(&self, id: hir::HirId) -> bool {
12081250
let node = self.tcx.hir().get(id);
12091251
matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. }))

library/core/src/alloc/layout.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use crate::cmp;
88
use crate::error::Error;
99
use crate::fmt;
10-
use crate::mem::{self, ValidAlign};
11-
use crate::ptr::NonNull;
10+
use crate::mem;
11+
use crate::ptr::{Alignment, NonNull};
1212

1313
// While this function is used in one place and its implementation
1414
// could be inlined, the previous attempts to do so made rustc
@@ -46,7 +46,7 @@ pub struct Layout {
4646
//
4747
// (However, we do not analogously require `align >= sizeof(void*)`,
4848
// even though that is *also* a requirement of `posix_memalign`.)
49-
align: ValidAlign,
49+
align: Alignment,
5050
}
5151

5252
impl Layout {
@@ -71,11 +71,11 @@ impl Layout {
7171
}
7272

7373
// SAFETY: just checked that align is a power of two.
74-
Layout::from_size_valid_align(size, unsafe { ValidAlign::new_unchecked(align) })
74+
Layout::from_size_alignment(size, unsafe { Alignment::new_unchecked(align) })
7575
}
7676

7777
#[inline(always)]
78-
const fn max_size_for_align(align: ValidAlign) -> usize {
78+
const fn max_size_for_align(align: Alignment) -> usize {
7979
// (power-of-two implies align != 0.)
8080

8181
// Rounded up size is:
@@ -95,7 +95,7 @@ impl Layout {
9595

9696
/// Internal helper constructor to skip revalidating alignment validity.
9797
#[inline]
98-
const fn from_size_valid_align(size: usize, align: ValidAlign) -> Result<Self, LayoutError> {
98+
const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
9999
if size > Self::max_size_for_align(align) {
100100
return Err(LayoutError);
101101
}
@@ -117,7 +117,7 @@ impl Layout {
117117
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
118118
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
119119
// SAFETY: the caller is required to uphold the preconditions.
120-
unsafe { Layout { size, align: ValidAlign::new_unchecked(align) } }
120+
unsafe { Layout { size, align: Alignment::new_unchecked(align) } }
121121
}
122122

123123
/// The minimum size in bytes for a memory block of this layout.
@@ -321,7 +321,7 @@ impl Layout {
321321
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
322322

323323
// The safe constructor is called here to enforce the isize size limit.
324-
Layout::from_size_valid_align(alloc_size, self.align).map(|layout| (layout, padded_size))
324+
Layout::from_size_alignment(alloc_size, self.align).map(|layout| (layout, padded_size))
325325
}
326326

327327
/// Creates a layout describing the record for `self` followed by
@@ -379,7 +379,7 @@ impl Layout {
379379
let new_size = offset.checked_add(next.size()).ok_or(LayoutError)?;
380380

381381
// The safe constructor is called here to enforce the isize size limit.
382-
let layout = Layout::from_size_valid_align(new_size, new_align)?;
382+
let layout = Layout::from_size_alignment(new_size, new_align)?;
383383
Ok((layout, offset))
384384
}
385385

@@ -400,7 +400,7 @@ impl Layout {
400400
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
401401
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
402402
// The safe constructor is called here to enforce the isize size limit.
403-
Layout::from_size_valid_align(size, self.align)
403+
Layout::from_size_alignment(size, self.align)
404404
}
405405

406406
/// Creates a layout describing the record for `self` followed by
@@ -414,7 +414,7 @@ impl Layout {
414414
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
415415
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
416416
// The safe constructor is called here to enforce the isize size limit.
417-
Layout::from_size_valid_align(new_size, self.align)
417+
Layout::from_size_alignment(new_size, self.align)
418418
}
419419

420420
/// Creates a layout describing the record for a `[T; n]`.
@@ -425,10 +425,10 @@ impl Layout {
425425
#[inline]
426426
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
427427
// Reduce the amount of code we need to monomorphize per `T`.
428-
return inner(mem::size_of::<T>(), ValidAlign::of::<T>(), n);
428+
return inner(mem::size_of::<T>(), Alignment::of::<T>(), n);
429429

430430
#[inline]
431-
fn inner(element_size: usize, align: ValidAlign, n: usize) -> Result<Layout, LayoutError> {
431+
fn inner(element_size: usize, align: Alignment, n: usize) -> Result<Layout, LayoutError> {
432432
// We need to check two things about the size:
433433
// - That the total size won't overflow a `usize`, and
434434
// - That the total size still fits in an `isize`.
@@ -443,7 +443,7 @@ impl Layout {
443443

444444
// SAFETY: We just checked above that the `array_size` will not
445445
// exceed `isize::MAX` even when rounded up to the alignment.
446-
// And `ValidAlign` guarantees it's a power of two.
446+
// And `Alignment` guarantees it's a power of two.
447447
unsafe { Ok(Layout::from_size_align_unchecked(array_size, align.as_usize())) }
448448
}
449449
}

library/core/src/mem/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ mod maybe_uninit;
2121
#[stable(feature = "maybe_uninit", since = "1.36.0")]
2222
pub use maybe_uninit::MaybeUninit;
2323

24-
// FIXME: This is left here for now to avoid complications around pending reverts.
25-
// Once <https://github.com/rust-lang/rust/issues/101899> is fully resolved,
26-
// this should be removed and the references in `alloc::Layout` updated.
27-
pub(crate) use ptr::Alignment as ValidAlign;
28-
2924
mod transmutability;
3025
#[unstable(feature = "transmutability", issue = "99571")]
3126
pub use transmutability::{Assume, BikeshedIntrinsicFrom};

library/core/src/num/int_macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ macro_rules! int_impl {
107107

108108
/// Returns the number of leading zeros in the binary representation of `self`.
109109
///
110+
/// Depending on what you're doing with the value, you might also be interested in the
111+
/// [`ilog2`] function which returns a consistent number, even if the type widens.
112+
///
110113
/// # Examples
111114
///
112115
/// Basic usage:
@@ -116,6 +119,7 @@ macro_rules! int_impl {
116119
///
117120
/// assert_eq!(n.leading_zeros(), 0);
118121
/// ```
122+
#[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
119123
#[stable(feature = "rust1", since = "1.0.0")]
120124
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
121125
#[must_use = "this returns the result of the operation, \

library/core/src/num/uint_macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ macro_rules! uint_impl {
109109

110110
/// Returns the number of leading zeros in the binary representation of `self`.
111111
///
112+
/// Depending on what you're doing with the value, you might also be interested in the
113+
/// [`ilog2`] function which returns a consistent number, even if the type widens.
114+
///
112115
/// # Examples
113116
///
114117
/// Basic usage:
@@ -118,6 +121,7 @@ macro_rules! uint_impl {
118121
///
119122
/// assert_eq!(n.leading_zeros(), 2);
120123
/// ```
124+
#[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
121125
#[stable(feature = "rust1", since = "1.0.0")]
122126
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
123127
#[must_use = "this returns the result of the operation, \

src/librustdoc/html/static/css/rustdoc.css

-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ h4.code-header {
183183
font-weight: 600;
184184
margin: 0;
185185
padding: 0;
186-
/* position notable traits in mobile mode within the header */
187-
position: relative;
188186
}
189187

190188
#crate-search,

src/librustdoc/html/static/css/themes/ayu.css

-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ details.rustdoc-toggle > summary::before {
160160
color: #788797;
161161
}
162162

163-
.search-failed a {
164-
color: #39AFD7;
165-
}
166-
167163
.tooltip::after {
168164
background-color: #314559;
169165
color: #c5c5c5;

src/librustdoc/html/static/css/themes/dark.css

-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ details.rustdoc-toggle > summary::before {
8282
filter: invert(100%);
8383
}
8484

85-
.search-failed a {
86-
color: #0089ff;
87-
}
88-
8985
.tooltip::after {
9086
background-color: #000;
9187
color: #fff;

src/librustdoc/html/static/css/themes/light.css

-5
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,12 @@
6969
--crate-search-hover-border: #717171;
7070
}
7171

72-
7372
.content .item-info::before { color: #ccc; }
7473

7574
body.source .example-wrap pre.rust a {
7675
background: #eee;
7776
}
7877

79-
.search-failed a {
80-
color: #3873AD;
81-
}
82-
8378
.tooltip::after {
8479
background-color: #000;
8580
color: #fff;

0 commit comments

Comments
 (0)