Skip to content

Commit 2480f8c

Browse files
authored
Merge branch 'rust-lang:master' into master
2 parents e484632 + 0ed85d0 commit 2480f8c

File tree

450 files changed

+4352
-2838
lines changed

Some content is hidden

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

450 files changed

+4352
-2838
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6332,9 +6332,9 @@ dependencies = [
63326332

63336333
[[package]]
63346334
name = "windows-bindgen"
6335-
version = "0.55.0"
6335+
version = "0.56.0"
63366336
source = "registry+https://github.com/rust-lang/crates.io-index"
6337-
checksum = "073ff8a486ebad239d557809d2cd5fe5e04ee1de29e09c6cd83fb0bae19b8a4c"
6337+
checksum = "a28e3ea6330cf17fdcdce8bf08d0549ce93769dca9bedc6c39c36c8c0e17db46"
63386338
dependencies = [
63396339
"proc-macro2",
63406340
"rayon",
@@ -6355,9 +6355,9 @@ dependencies = [
63556355

63566356
[[package]]
63576357
name = "windows-metadata"
6358-
version = "0.55.0"
6358+
version = "0.56.0"
63596359
source = "registry+https://github.com/rust-lang/crates.io-index"
6360-
checksum = "b602635050172a1fc57a35040d4d225baefc6098fefd97094919921d95961a7d"
6360+
checksum = "3993f7827fff10c454e3a24847075598c7c08108304b8b07943c2c73d78f3b34"
63616361

63626362
[[package]]
63636363
name = "windows-sys"

compiler/rustc_ast/src/ast.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl Pat {
568568
// In a type expression `_` is an inference variable.
569569
PatKind::Wild => TyKind::Infer,
570570
// An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
571-
PatKind::Ident(BindingAnnotation::NONE, ident, None) => {
571+
PatKind::Ident(BindingMode::NONE, ident, None) => {
572572
TyKind::Path(None, Path::from_ident(*ident))
573573
}
574574
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
@@ -675,7 +675,7 @@ impl Pat {
675675
pub fn descr(&self) -> Option<String> {
676676
match &self.kind {
677677
PatKind::Wild => Some("_".to_string()),
678-
PatKind::Ident(BindingAnnotation::NONE, ident, None) => Some(format!("{ident}")),
678+
PatKind::Ident(BindingMode::NONE, ident, None) => Some(format!("{ident}")),
679679
PatKind::Ref(pat, mutbl) => pat.descr().map(|d| format!("&{}{d}", mutbl.prefix_str())),
680680
_ => None,
681681
}
@@ -707,14 +707,25 @@ pub enum ByRef {
707707
No,
708708
}
709709

710-
/// Explicit binding annotations given in the HIR for a binding. Note
711-
/// that this is not the final binding *mode* that we infer after type
712-
/// inference.
710+
impl ByRef {
711+
pub fn cap_ref_mutability(mut self, mutbl: Mutability) -> Self {
712+
if let ByRef::Yes(old_mutbl) = &mut self {
713+
*old_mutbl = cmp::min(*old_mutbl, mutbl);
714+
}
715+
self
716+
}
717+
}
718+
719+
/// The mode of a binding (`mut`, `ref mut`, etc).
720+
/// Used for both the explicit binding annotations given in the HIR for a binding
721+
/// and the final binding mode that we infer after type inference/match ergonomics.
722+
/// `.0` is the by-reference mode (`ref`, `ref mut`, or by value),
723+
/// `.1` is the mutability of the binding.
713724
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
714725
#[derive(Encodable, Decodable, HashStable_Generic)]
715-
pub struct BindingAnnotation(pub ByRef, pub Mutability);
726+
pub struct BindingMode(pub ByRef, pub Mutability);
716727

717-
impl BindingAnnotation {
728+
impl BindingMode {
718729
pub const NONE: Self = Self(ByRef::No, Mutability::Not);
719730
pub const REF: Self = Self(ByRef::Yes(Mutability::Not), Mutability::Not);
720731
pub const MUT: Self = Self(ByRef::No, Mutability::Mut);
@@ -732,13 +743,6 @@ impl BindingAnnotation {
732743
Self::MUT_REF_MUT => "mut ref mut ",
733744
}
734745
}
735-
736-
pub fn cap_ref_mutability(mut self, mutbl: Mutability) -> Self {
737-
if let ByRef::Yes(old_mutbl) = &mut self.0 {
738-
*old_mutbl = cmp::min(*old_mutbl, mutbl);
739-
}
740-
self
741-
}
742746
}
743747

744748
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -769,7 +773,7 @@ pub enum PatKind {
769773
/// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
770774
/// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
771775
/// during name resolution.
772-
Ident(BindingAnnotation, Ident, Option<P<Pat>>),
776+
Ident(BindingMode, Ident, Option<P<Pat>>),
773777

774778
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
775779
Struct(Option<P<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),
@@ -2382,7 +2386,7 @@ pub type ExplicitSelf = Spanned<SelfKind>;
23822386
impl Param {
23832387
/// Attempts to cast parameter to `ExplicitSelf`.
23842388
pub fn to_self(&self) -> Option<ExplicitSelf> {
2385-
if let PatKind::Ident(BindingAnnotation(ByRef::No, mutbl), ident, _) = self.pat.kind {
2389+
if let PatKind::Ident(BindingMode(ByRef::No, mutbl), ident, _) = self.pat.kind {
23862390
if ident.name == kw::SelfLower {
23872391
return match self.ty.kind {
23882392
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
@@ -2434,7 +2438,7 @@ impl Param {
24342438
attrs,
24352439
pat: P(Pat {
24362440
id: DUMMY_NODE_ID,
2437-
kind: PatKind::Ident(BindingAnnotation(ByRef::No, mutbl), eself_ident, None),
2441+
kind: PatKind::Ident(BindingMode(ByRef::No, mutbl), eself_ident, None),
24382442
span,
24392443
tokens: None,
24402444
}),
@@ -3363,7 +3367,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {
33633367
pub type ForeignItem = Item<ForeignItemKind>;
33643368

33653369
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
3366-
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), target_pointer_width = "64"))]
3370+
#[cfg(target_pointer_width = "64")]
33673371
mod size_asserts {
33683372
use super::*;
33693373
use rustc_data_structures::static_assert_size;

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ where
10231023
}
10241024

10251025
// Some types are used a lot. Make sure they don't unintentionally get bigger.
1026-
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), target_pointer_width = "64"))]
1026+
#[cfg(target_pointer_width = "64")]
10271027
mod size_asserts {
10281028
use super::*;
10291029
use rustc_data_structures::static_assert_size;

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl DelimSpacing {
768768
}
769769

770770
// Some types are used a lot. Make sure they don't unintentionally get bigger.
771-
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), target_pointer_width = "64"))]
771+
#[cfg(target_pointer_width = "64")]
772772
mod size_asserts {
773773
use super::*;
774774
use rustc_data_structures::static_assert_size;

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
178178
let pat_id = self.lower_node_id(pat_node_id);
179179
let pat = self.arena.alloc(hir::Pat {
180180
hir_id: pat_id,
181-
kind: hir::PatKind::Binding(hir::BindingAnnotation::NONE, pat_id, Ident::empty(), None),
181+
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, Ident::empty(), None),
182182
span: ty.span,
183183
default_binding_modes: false,
184184
});

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
643643
let (pat, task_context_hid) = self.pat_ident_binding_mode(
644644
span,
645645
Ident::with_dummy_span(sym::_task_context),
646-
hir::BindingAnnotation::MUT,
646+
hir::BindingMode::MUT,
647647
);
648648
let param = hir::Param {
649649
hir_id: self.next_id(),
@@ -805,11 +805,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
805805
// debuggers and debugger extensions expect it to be called `__awaitee`. They use
806806
// this name to identify what is being awaited by a suspended async functions.
807807
let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
808-
let (awaitee_pat, awaitee_pat_hid) = self.pat_ident_binding_mode(
809-
gen_future_span,
810-
awaitee_ident,
811-
hir::BindingAnnotation::MUT,
812-
);
808+
let (awaitee_pat, awaitee_pat_hid) =
809+
self.pat_ident_binding_mode(gen_future_span, awaitee_ident, hir::BindingMode::MUT);
813810

814811
let task_context_ident = Ident::with_dummy_span(sym::_task_context);
815812

@@ -1648,7 +1645,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16481645
// `mut iter`
16491646
let iter = Ident::with_dummy_span(sym::iter);
16501647
let (iter_pat, iter_pat_nid) =
1651-
self.pat_ident_binding_mode(head_span, iter, hir::BindingAnnotation::MUT);
1648+
self.pat_ident_binding_mode(head_span, iter, hir::BindingMode::MUT);
16521649

16531650
let match_expr = {
16541651
let iter = self.expr_ident(head_span, iter, iter_pat_nid);

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 117 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,126 @@ impl<'hir> LoweringContext<'_, 'hir> {
2020
let mut fmt = Cow::Borrowed(fmt);
2121
if self.tcx.sess.opts.unstable_opts.flatten_format_args {
2222
fmt = flatten_format_args(fmt);
23-
fmt = inline_literals(fmt);
23+
fmt = self.inline_literals(fmt);
2424
}
2525
expand_format_args(self, sp, &fmt, allow_const)
2626
}
27+
28+
/// Try to convert a literal into an interned string
29+
fn try_inline_lit(&self, lit: token::Lit) -> Option<Symbol> {
30+
match LitKind::from_token_lit(lit) {
31+
Ok(LitKind::Str(s, _)) => Some(s),
32+
Ok(LitKind::Int(n, ty)) => {
33+
match ty {
34+
// unsuffixed integer literals are assumed to be i32's
35+
LitIntType::Unsuffixed => {
36+
(n <= i32::MAX as u128).then_some(Symbol::intern(&n.to_string()))
37+
}
38+
LitIntType::Signed(int_ty) => {
39+
let max_literal = self.int_ty_max(int_ty);
40+
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
41+
}
42+
LitIntType::Unsigned(uint_ty) => {
43+
let max_literal = self.uint_ty_max(uint_ty);
44+
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
45+
}
46+
}
47+
}
48+
_ => None,
49+
}
50+
}
51+
52+
/// Get the maximum value of int_ty. It is platform-dependent due to the byte size of isize
53+
fn int_ty_max(&self, int_ty: IntTy) -> u128 {
54+
match int_ty {
55+
IntTy::Isize => self.tcx.data_layout.pointer_size.signed_int_max() as u128,
56+
IntTy::I8 => i8::MAX as u128,
57+
IntTy::I16 => i16::MAX as u128,
58+
IntTy::I32 => i32::MAX as u128,
59+
IntTy::I64 => i64::MAX as u128,
60+
IntTy::I128 => i128::MAX as u128,
61+
}
62+
}
63+
64+
/// Get the maximum value of uint_ty. It is platform-dependent due to the byte size of usize
65+
fn uint_ty_max(&self, uint_ty: UintTy) -> u128 {
66+
match uint_ty {
67+
UintTy::Usize => self.tcx.data_layout.pointer_size.unsigned_int_max(),
68+
UintTy::U8 => u8::MAX as u128,
69+
UintTy::U16 => u16::MAX as u128,
70+
UintTy::U32 => u32::MAX as u128,
71+
UintTy::U64 => u64::MAX as u128,
72+
UintTy::U128 => u128::MAX as u128,
73+
}
74+
}
75+
76+
/// Inline literals into the format string.
77+
///
78+
/// Turns
79+
///
80+
/// `format_args!("Hello, {}! {} {}", "World", 123, x)`
81+
///
82+
/// into
83+
///
84+
/// `format_args!("Hello, World! 123 {}", x)`.
85+
fn inline_literals<'fmt>(&self, mut fmt: Cow<'fmt, FormatArgs>) -> Cow<'fmt, FormatArgs> {
86+
let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
87+
let mut inlined_anything = false;
88+
89+
for i in 0..fmt.template.len() {
90+
let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
91+
let Ok(arg_index) = placeholder.argument.index else { continue };
92+
93+
let mut literal = None;
94+
95+
if let FormatTrait::Display = placeholder.format_trait
96+
&& placeholder.format_options == Default::default()
97+
&& let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
98+
&& let ExprKind::Lit(lit) = arg.kind
99+
{
100+
literal = self.try_inline_lit(lit);
101+
}
102+
103+
if let Some(literal) = literal {
104+
// Now we need to mutate the outer FormatArgs.
105+
// If this is the first time, this clones the outer FormatArgs.
106+
let fmt = fmt.to_mut();
107+
// Replace the placeholder with the literal.
108+
fmt.template[i] = FormatArgsPiece::Literal(literal);
109+
was_inlined[arg_index] = true;
110+
inlined_anything = true;
111+
}
112+
}
113+
114+
// Remove the arguments that were inlined.
115+
if inlined_anything {
116+
let fmt = fmt.to_mut();
117+
118+
let mut remove = was_inlined;
119+
120+
// Don't remove anything that's still used.
121+
for_all_argument_indexes(&mut fmt.template, |index| remove[*index] = false);
122+
123+
// Drop all the arguments that are marked for removal.
124+
let mut remove_it = remove.iter();
125+
fmt.arguments.all_args_mut().retain(|_| remove_it.next() != Some(&true));
126+
127+
// Calculate the mapping of old to new indexes for the remaining arguments.
128+
let index_map: Vec<usize> = remove
129+
.into_iter()
130+
.scan(0, |i, remove| {
131+
let mapped = *i;
132+
*i += !remove as usize;
133+
Some(mapped)
134+
})
135+
.collect();
136+
137+
// Correct the indexes that refer to arguments that have shifted position.
138+
for_all_argument_indexes(&mut fmt.template, |index| *index = index_map[*index]);
139+
}
140+
141+
fmt
142+
}
27143
}
28144

29145
/// Flattens nested `format_args!()` into one.
@@ -103,82 +219,6 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
103219
fmt
104220
}
105221

106-
/// Inline literals into the format string.
107-
///
108-
/// Turns
109-
///
110-
/// `format_args!("Hello, {}! {} {}", "World", 123, x)`
111-
///
112-
/// into
113-
///
114-
/// `format_args!("Hello, World! 123 {}", x)`.
115-
fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
116-
let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
117-
let mut inlined_anything = false;
118-
119-
for i in 0..fmt.template.len() {
120-
let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
121-
let Ok(arg_index) = placeholder.argument.index else { continue };
122-
123-
let mut literal = None;
124-
125-
if let FormatTrait::Display = placeholder.format_trait
126-
&& placeholder.format_options == Default::default()
127-
&& let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
128-
&& let ExprKind::Lit(lit) = arg.kind
129-
{
130-
if let token::LitKind::Str | token::LitKind::StrRaw(_) = lit.kind
131-
&& let Ok(LitKind::Str(s, _)) = LitKind::from_token_lit(lit)
132-
{
133-
literal = Some(s);
134-
} else if let token::LitKind::Integer = lit.kind
135-
&& let Ok(LitKind::Int(n, _)) = LitKind::from_token_lit(lit)
136-
{
137-
literal = Some(Symbol::intern(&n.to_string()));
138-
}
139-
}
140-
141-
if let Some(literal) = literal {
142-
// Now we need to mutate the outer FormatArgs.
143-
// If this is the first time, this clones the outer FormatArgs.
144-
let fmt = fmt.to_mut();
145-
// Replace the placeholder with the literal.
146-
fmt.template[i] = FormatArgsPiece::Literal(literal);
147-
was_inlined[arg_index] = true;
148-
inlined_anything = true;
149-
}
150-
}
151-
152-
// Remove the arguments that were inlined.
153-
if inlined_anything {
154-
let fmt = fmt.to_mut();
155-
156-
let mut remove = was_inlined;
157-
158-
// Don't remove anything that's still used.
159-
for_all_argument_indexes(&mut fmt.template, |index| remove[*index] = false);
160-
161-
// Drop all the arguments that are marked for removal.
162-
let mut remove_it = remove.iter();
163-
fmt.arguments.all_args_mut().retain(|_| remove_it.next() != Some(&true));
164-
165-
// Calculate the mapping of old to new indexes for the remaining arguments.
166-
let index_map: Vec<usize> = remove
167-
.into_iter()
168-
.scan(0, |i, remove| {
169-
let mapped = *i;
170-
*i += !remove as usize;
171-
Some(mapped)
172-
})
173-
.collect();
174-
175-
// Correct the indexes that refer to arguments that have shifted position.
176-
for_all_argument_indexes(&mut fmt.template, |index| *index = index_map[*index]);
177-
}
178-
179-
fmt
180-
}
181-
182222
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
183223
enum ArgumentType {
184224
Format(FormatTrait),

0 commit comments

Comments
 (0)