Skip to content

Commit 3b63948

Browse files
committed
Auto merge of #107546 - matthiaskrgr:rollup-9rgf2gx, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #107389 (Fixing confusion between mod and remainder) - #107442 (improve panic message for slice windows and chunks) - #107470 (Small bootstrap improvements) - #107487 (Make the "extra if in let...else block" hint a suggestion) - #107499 (Do not depend on Generator trait when deducing closure signature) - #107533 (Extend `-Z print-type-sizes` to distinguish generator upvars+locals from "normal" fields.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0d32c8f + f41f154 commit 3b63948

24 files changed

+289
-267
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::ty::visit::TypeVisitable;
1515
use rustc_middle::ty::{self, Ty, TypeSuperVisitable, TypeVisitor};
1616
use rustc_span::def_id::LocalDefId;
1717
use rustc_span::source_map::Span;
18+
use rustc_span::sym;
1819
use rustc_target::spec::abi::Abi;
1920
use rustc_trait_selection::traits;
2021
use rustc_trait_selection::traits::error_reporting::ArgKind;
@@ -288,21 +289,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
288289
let trait_def_id = projection.trait_def_id(tcx);
289290

290291
let is_fn = tcx.is_fn_trait(trait_def_id);
291-
let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span);
292-
let is_gen = gen_trait == trait_def_id;
292+
293+
let gen_trait = tcx.lang_items().gen_trait();
294+
let is_gen = gen_trait == Some(trait_def_id);
295+
293296
if !is_fn && !is_gen {
294297
debug!("not fn or generator");
295298
return None;
296299
}
297300

298-
if is_gen {
299-
// Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
300-
// associated item and not yield.
301-
let return_assoc_item = self.tcx.associated_item_def_ids(gen_trait)[1];
302-
if return_assoc_item != projection.projection_def_id() {
303-
debug!("not return assoc item of generator");
304-
return None;
305-
}
301+
// Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
302+
// associated item and not yield.
303+
if is_gen && self.tcx.associated_item(projection.projection_def_id()).name != sym::Return {
304+
debug!("not `Return` assoc item of `Generator`");
305+
return None;
306306
}
307307

308308
let input_tys = if is_fn {

compiler/rustc_hir_typeck/src/op.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335335
format!("cannot divide `{lhs_ty}` by `{rhs_ty}`")
336336
}
337337
hir::BinOpKind::Rem => {
338-
format!("cannot mod `{lhs_ty}` by `{rhs_ty}`")
338+
format!(
339+
"cannot calculate the remainder of `{lhs_ty}` divided by `{rhs_ty}`"
340+
)
339341
}
340342
hir::BinOpKind::BitAnd => {
341343
format!("no implementation for `{lhs_ty} & {rhs_ty}`")

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub(crate) enum IfExpressionMissingThenBlockSub {
351351
}
352352

353353
#[derive(Subdiagnostic)]
354-
#[help(parse_extra_if_in_let_else)]
354+
#[suggestion(parse_extra_if_in_let_else, applicability = "maybe-incorrect", code = "")]
355355
pub(crate) struct IfExpressionLetSomeSub {
356356
#[primary_span]
357357
pub if_span: Span,

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ impl<'a> Parser<'a> {
22902290
block
22912291
} else {
22922292
let let_else_sub = matches!(cond.kind, ExprKind::Let(..))
2293-
.then(|| IfExpressionLetSomeSub { if_span: lo });
2293+
.then(|| IfExpressionLetSomeSub { if_span: lo.until(cond_span) });
22942294

22952295
self.sess.emit_err(IfExpressionMissingThenBlock {
22962296
if_span: lo,

compiler/rustc_session/src/code_stats.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ pub enum SizeKind {
1919
Min,
2020
}
2121

22+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
23+
pub enum FieldKind {
24+
AdtField,
25+
Upvar,
26+
GeneratorLocal,
27+
}
28+
29+
impl std::fmt::Display for FieldKind {
30+
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31+
match self {
32+
FieldKind::AdtField => write!(w, "field"),
33+
FieldKind::Upvar => write!(w, "upvar"),
34+
FieldKind::GeneratorLocal => write!(w, "local"),
35+
}
36+
}
37+
}
38+
2239
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
2340
pub struct FieldInfo {
41+
pub kind: FieldKind,
2442
pub name: Symbol,
2543
pub offset: u64,
2644
pub size: u64,
@@ -145,7 +163,7 @@ impl CodeStats {
145163
fields.sort_by_key(|f| (f.offset, f.size));
146164

147165
for field in fields {
148-
let FieldInfo { ref name, offset, size, align } = field;
166+
let FieldInfo { kind, ref name, offset, size, align } = field;
149167

150168
if offset > min_offset {
151169
let pad = offset - min_offset;
@@ -155,16 +173,16 @@ impl CodeStats {
155173
if offset < min_offset {
156174
// If this happens it's probably a union.
157175
println!(
158-
"print-type-size {indent}field `.{name}`: {size} bytes, \
176+
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
159177
offset: {offset} bytes, \
160178
alignment: {align} bytes"
161179
);
162180
} else if info.packed || offset == min_offset {
163-
println!("print-type-size {indent}field `.{name}`: {size} bytes");
181+
println!("print-type-size {indent}{kind} `.{name}`: {size} bytes");
164182
} else {
165183
// Include field alignment in output only if it caused padding injection
166184
println!(
167-
"print-type-size {indent}field `.{name}`: {size} bytes, \
185+
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
168186
alignment: {align} bytes"
169187
);
170188
}

compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cgu_reuse_tracker::CguReuseTracker;
22
use crate::code_stats::CodeStats;
3-
pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
3+
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
44
use crate::config::Input;
55
use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath};
66
use crate::errors::{

compiler/rustc_ty_utils/src/layout.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::layout::{
99
use rustc_middle::ty::{
1010
self, subst::SubstsRef, AdtDef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitable,
1111
};
12-
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
12+
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
1313
use rustc_span::symbol::Symbol;
1414
use rustc_span::DUMMY_SP;
1515
use rustc_target::abi::*;
@@ -881,6 +881,7 @@ fn variant_info_for_adt<'tcx>(
881881
let offset = layout.fields.offset(i);
882882
min_size = min_size.max(offset + field_layout.size);
883883
FieldInfo {
884+
kind: FieldKind::AdtField,
884885
name,
885886
offset: offset.bytes(),
886887
size: field_layout.size.bytes(),
@@ -960,6 +961,7 @@ fn variant_info_for_generator<'tcx>(
960961
let offset = layout.fields.offset(field_idx);
961962
upvars_size = upvars_size.max(offset + field_layout.size);
962963
FieldInfo {
964+
kind: FieldKind::Upvar,
963965
name: Symbol::intern(&name),
964966
offset: offset.bytes(),
965967
size: field_layout.size.bytes(),
@@ -983,6 +985,7 @@ fn variant_info_for_generator<'tcx>(
983985
// The struct is as large as the last field's end
984986
variant_size = variant_size.max(offset + field_layout.size);
985987
FieldInfo {
988+
kind: FieldKind::GeneratorLocal,
986989
name: state_specific_names.get(*local).copied().flatten().unwrap_or(
987990
Symbol::intern(&format!(".generator_field{}", local.as_usize())),
988991
),

library/core/src/ops/arith.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ div_impl_float! { f32 f64 }
545545
#[lang = "rem"]
546546
#[stable(feature = "rust1", since = "1.0.0")]
547547
#[rustc_on_unimplemented(
548-
message = "cannot mod `{Self}` by `{Rhs}`",
548+
message = "cannot calculate the remainder of `{Self}` divided by `{Rhs}`",
549549
label = "no implementation for `{Self} % {Rhs}`"
550550
)]
551551
#[doc(alias = "%")]
@@ -981,7 +981,7 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
981981
#[lang = "rem_assign"]
982982
#[stable(feature = "op_assign_traits", since = "1.8.0")]
983983
#[rustc_on_unimplemented(
984-
message = "cannot mod-assign `{Self}` by `{Rhs}``",
984+
message = "cannot calculate and assign the remainder of `{Self}` divided by `{Rhs}`",
985985
label = "no implementation for `{Self} %= {Rhs}`"
986986
)]
987987
#[doc(alias = "%")]

library/core/src/slice/mod.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,9 @@ impl<T> [T] {
805805
/// ```
806806
#[stable(feature = "rust1", since = "1.0.0")]
807807
#[inline]
808+
#[track_caller]
808809
pub fn windows(&self, size: usize) -> Windows<'_, T> {
809-
let size = NonZeroUsize::new(size).expect("size is zero");
810+
let size = NonZeroUsize::new(size).expect("window size must be non-zero");
810811
Windows::new(self, size)
811812
}
812813

@@ -839,8 +840,9 @@ impl<T> [T] {
839840
/// [`rchunks`]: slice::rchunks
840841
#[stable(feature = "rust1", since = "1.0.0")]
841842
#[inline]
843+
#[track_caller]
842844
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
843-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
845+
assert!(chunk_size != 0, "chunk size must be non-zero");
844846
Chunks::new(self, chunk_size)
845847
}
846848

@@ -877,8 +879,9 @@ impl<T> [T] {
877879
/// [`rchunks_mut`]: slice::rchunks_mut
878880
#[stable(feature = "rust1", since = "1.0.0")]
879881
#[inline]
882+
#[track_caller]
880883
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
881-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
884+
assert!(chunk_size != 0, "chunk size must be non-zero");
882885
ChunksMut::new(self, chunk_size)
883886
}
884887

@@ -914,8 +917,9 @@ impl<T> [T] {
914917
/// [`rchunks_exact`]: slice::rchunks_exact
915918
#[stable(feature = "chunks_exact", since = "1.31.0")]
916919
#[inline]
920+
#[track_caller]
917921
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
918-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
922+
assert!(chunk_size != 0, "chunk size must be non-zero");
919923
ChunksExact::new(self, chunk_size)
920924
}
921925

@@ -956,8 +960,9 @@ impl<T> [T] {
956960
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
957961
#[stable(feature = "chunks_exact", since = "1.31.0")]
958962
#[inline]
963+
#[track_caller]
959964
pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
960-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
965+
assert!(chunk_size != 0, "chunk size must be non-zero");
961966
ChunksExactMut::new(self, chunk_size)
962967
}
963968

@@ -1037,9 +1042,10 @@ impl<T> [T] {
10371042
/// ```
10381043
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10391044
#[inline]
1045+
#[track_caller]
10401046
#[must_use]
10411047
pub fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
1042-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1048+
assert!(N != 0, "chunk size must be non-zero");
10431049
let len = self.len() / N;
10441050
let (multiple_of_n, remainder) = self.split_at(len * N);
10451051
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1068,9 +1074,10 @@ impl<T> [T] {
10681074
/// ```
10691075
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10701076
#[inline]
1077+
#[track_caller]
10711078
#[must_use]
10721079
pub fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
1073-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1080+
assert!(N != 0, "chunk size must be non-zero");
10741081
let len = self.len() / N;
10751082
let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
10761083
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1108,8 +1115,9 @@ impl<T> [T] {
11081115
/// [`chunks_exact`]: slice::chunks_exact
11091116
#[unstable(feature = "array_chunks", issue = "74985")]
11101117
#[inline]
1118+
#[track_caller]
11111119
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
1112-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1120+
assert!(N != 0, "chunk size must be non-zero");
11131121
ArrayChunks::new(self)
11141122
}
11151123

@@ -1186,9 +1194,10 @@ impl<T> [T] {
11861194
/// ```
11871195
#[unstable(feature = "slice_as_chunks", issue = "74985")]
11881196
#[inline]
1197+
#[track_caller]
11891198
#[must_use]
11901199
pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
1191-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1200+
assert!(N != 0, "chunk size must be non-zero");
11921201
let len = self.len() / N;
11931202
let (multiple_of_n, remainder) = self.split_at_mut(len * N);
11941203
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1223,9 +1232,10 @@ impl<T> [T] {
12231232
/// ```
12241233
#[unstable(feature = "slice_as_chunks", issue = "74985")]
12251234
#[inline]
1235+
#[track_caller]
12261236
#[must_use]
12271237
pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
1228-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1238+
assert!(N != 0, "chunk size must be non-zero");
12291239
let len = self.len() / N;
12301240
let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
12311241
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1265,8 +1275,9 @@ impl<T> [T] {
12651275
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
12661276
#[unstable(feature = "array_chunks", issue = "74985")]
12671277
#[inline]
1278+
#[track_caller]
12681279
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
1269-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1280+
assert!(N != 0, "chunk size must be non-zero");
12701281
ArrayChunksMut::new(self)
12711282
}
12721283

@@ -1297,8 +1308,9 @@ impl<T> [T] {
12971308
/// [`windows`]: slice::windows
12981309
#[unstable(feature = "array_windows", issue = "75027")]
12991310
#[inline]
1311+
#[track_caller]
13001312
pub fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1301-
assert_ne!(N, 0, "windows cannot have a size of zero");
1313+
assert!(N != 0, "window size must be non-zero");
13021314
ArrayWindows::new(self)
13031315
}
13041316

@@ -1331,8 +1343,9 @@ impl<T> [T] {
13311343
/// [`chunks`]: slice::chunks
13321344
#[stable(feature = "rchunks", since = "1.31.0")]
13331345
#[inline]
1346+
#[track_caller]
13341347
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
1335-
assert!(chunk_size != 0);
1348+
assert!(chunk_size != 0, "chunk size must be non-zero");
13361349
RChunks::new(self, chunk_size)
13371350
}
13381351

@@ -1369,8 +1382,9 @@ impl<T> [T] {
13691382
/// [`chunks_mut`]: slice::chunks_mut
13701383
#[stable(feature = "rchunks", since = "1.31.0")]
13711384
#[inline]
1385+
#[track_caller]
13721386
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
1373-
assert!(chunk_size != 0);
1387+
assert!(chunk_size != 0, "chunk size must be non-zero");
13741388
RChunksMut::new(self, chunk_size)
13751389
}
13761390

@@ -1408,8 +1422,9 @@ impl<T> [T] {
14081422
/// [`chunks_exact`]: slice::chunks_exact
14091423
#[stable(feature = "rchunks", since = "1.31.0")]
14101424
#[inline]
1425+
#[track_caller]
14111426
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
1412-
assert!(chunk_size != 0);
1427+
assert!(chunk_size != 0, "chunk size must be non-zero");
14131428
RChunksExact::new(self, chunk_size)
14141429
}
14151430

@@ -1451,8 +1466,9 @@ impl<T> [T] {
14511466
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
14521467
#[stable(feature = "rchunks", since = "1.31.0")]
14531468
#[inline]
1469+
#[track_caller]
14541470
pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
1455-
assert!(chunk_size != 0);
1471+
assert!(chunk_size != 0, "chunk size must be non-zero");
14561472
RChunksExactMut::new(self, chunk_size)
14571473
}
14581474

0 commit comments

Comments
 (0)