Skip to content

Commit 0a84708

Browse files
committed
Auto merge of #88535 - m-ou-se:rollup-jeusxbo, r=m-ou-se
Rollup of 10 pull requests Successful merges: - #85017 (Add carrying_add, borrowing_sub, widening_mul, carrying_mul methods to integers) - #86362 (Avoid cloning LocalDecls) - #88391 (Fix json tuple struct enum variant ) - #88399 (Disallow the aapcs CC on Aarch64) - #88418 (Allow `~const` bounds on trait assoc functions) - #88445 (Clean up the lowering of AST items) - #88495 (Add `TcpStream::set_linger` and `TcpStream::linger`) - #88501 (Use right span in prelude collision suggestions with macros. ) - #88504 (Keep turbofish in prelude collision lint.) - #88524 (Remove unnecessary `mut` from udp doctests) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 76d18cf + f5cf967 commit 0a84708

Some content is hidden

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

48 files changed

+1008
-128
lines changed

compiler/rustc_ast_lowering/src/item.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,43 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2626
}
2727

2828
impl ItemLowerer<'_, '_, '_> {
29-
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) {
29+
fn with_trait_impl_ref<T>(
30+
&mut self,
31+
impl_ref: &Option<TraitRef>,
32+
f: impl FnOnce(&mut Self) -> T,
33+
) -> T {
3034
let old = self.lctx.is_in_trait_impl;
3135
self.lctx.is_in_trait_impl = impl_ref.is_some();
32-
f(self);
36+
let ret = f(self);
3337
self.lctx.is_in_trait_impl = old;
38+
ret
3439
}
3540
}
3641

3742
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
3843
fn visit_item(&mut self, item: &'a Item) {
39-
let mut item_hir_id = None;
40-
self.lctx.with_hir_id_owner(item.id, |lctx| {
44+
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
4145
lctx.without_in_scope_lifetime_defs(|lctx| {
42-
if let Some(hir_item) = lctx.lower_item(item) {
43-
let id = lctx.insert_item(hir_item);
44-
item_hir_id = Some(id);
45-
}
46+
let hir_item = lctx.lower_item(item);
47+
lctx.insert_item(hir_item)
4648
})
4749
});
4850

49-
if let Some(hir_id) = item_hir_id {
50-
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
51-
let this = &mut ItemLowerer { lctx: this };
52-
match item.kind {
53-
ItemKind::Mod(..) => {
54-
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
55-
let old_current_module =
56-
mem::replace(&mut this.lctx.current_module, def_id);
57-
visit::walk_item(this, item);
58-
this.lctx.current_module = old_current_module;
59-
}
60-
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
61-
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
62-
}
63-
_ => visit::walk_item(this, item),
51+
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
52+
let this = &mut ItemLowerer { lctx: this };
53+
match item.kind {
54+
ItemKind::Mod(..) => {
55+
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
56+
let old_current_module = mem::replace(&mut this.lctx.current_module, def_id);
57+
visit::walk_item(this, item);
58+
this.lctx.current_module = old_current_module;
6459
}
65-
});
66-
}
60+
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
61+
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
62+
}
63+
_ => visit::walk_item(this, item),
64+
}
65+
});
6766
}
6867

6968
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
@@ -113,7 +112,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
113112
fn with_parent_item_lifetime_defs<T>(
114113
&mut self,
115114
parent_hir_id: hir::ItemId,
116-
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
115+
f: impl FnOnce(&mut Self) -> T,
117116
) -> T {
118117
let old_len = self.in_scope_lifetimes.len();
119118

@@ -137,10 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
137136
// Clears (and restores) the `in_scope_lifetimes` field. Used when
138137
// visiting nested items, which never inherit in-scope lifetimes
139138
// from their surrounding environment.
140-
fn without_in_scope_lifetime_defs<T>(
141-
&mut self,
142-
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
143-
) -> T {
139+
fn without_in_scope_lifetime_defs<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
144140
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
145141

146142
// this vector is only used when walking over impl headers,
@@ -208,19 +204,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
208204
}
209205
}
210206

211-
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
207+
pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> {
212208
let mut ident = i.ident;
213209
let mut vis = self.lower_visibility(&i.vis, None);
214210
let hir_id = self.lower_node_id(i.id);
215211
let attrs = self.lower_attrs(hir_id, &i.attrs);
216212
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
217-
Some(hir::Item {
213+
hir::Item {
218214
def_id: hir_id.expect_owner(),
219215
ident: self.lower_ident(ident),
220216
kind,
221217
vis,
222218
span: self.lower_span(i.span),
223-
})
219+
}
224220
}
225221

226222
fn lower_item_kind(

compiler/rustc_ast_passes/src/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14421442
if !self.is_tilde_const_allowed {
14431443
self.err_handler()
14441444
.struct_span_err(bound.span(), "`~const` is not allowed here")
1445-
.note("only allowed on bounds on traits' associated types, const fns, const impls and its associated functions")
1445+
.note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions")
14461446
.emit();
14471447
}
14481448
}
@@ -1616,7 +1616,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16161616
walk_list!(self, visit_ty, ty);
16171617
}
16181618
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body))
1619-
if self.in_const_trait_impl =>
1619+
if self.in_const_trait_impl || ctxt == AssocCtxt::Trait =>
16201620
{
16211621
self.visit_vis(&item.vis);
16221622
self.visit_ident(item.ident);

compiler/rustc_middle/src/mir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ impl<'tcx> Body<'tcx> {
426426
(arg_count + 1..local_count).map(Local::new)
427427
}
428428

429+
#[inline]
430+
pub fn drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a {
431+
self.local_decls.drain(self.arg_count + 1..)
432+
}
433+
429434
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
430435
/// invalidating statement indices in `Location`s.
431436
pub fn make_statement_nop(&mut self, location: Location) {

compiler/rustc_mir/src/transform/inline.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,7 @@ impl Inliner<'tcx> {
607607
}
608608

609609
// Insert all of the (mapped) parts of the callee body into the caller.
610-
caller_body.local_decls.extend(
611-
// FIXME(eddyb) make `Range<Local>` iterable so that we can use
612-
// `callee_body.local_decls.drain(callee_body.vars_and_temps())`
613-
callee_body
614-
.vars_and_temps_iter()
615-
.map(|local| callee_body.local_decls[local].clone()),
616-
);
610+
caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
617611
caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
618612
caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
619613
caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));

compiler/rustc_span/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,14 @@ impl Span {
597597
if !expn_data.is_root() { Some(expn_data.call_site) } else { None }
598598
}
599599

600+
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
601+
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
602+
while !outer.contains(self) {
603+
self = self.parent()?;
604+
}
605+
Some(self)
606+
}
607+
600608
/// Edition of the crate from which this span came.
601609
pub fn edition(self) -> edition::Edition {
602610
self.ctxt().edition()

compiler/rustc_target/src/spec/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,8 @@ impl Target {
15011501
| Cdecl
15021502
| EfiApi => true,
15031503
X86Interrupt => ["x86", "x86_64"].contains(&&self.arch[..]),
1504-
Aapcs | CCmseNonSecureCall => ["arm", "aarch64"].contains(&&self.arch[..]),
1504+
Aapcs => "arm" == self.arch,
1505+
CCmseNonSecureCall => ["arm", "aarch64"].contains(&&self.arch[..]),
15051506
Win64 | SysV64 => self.arch == "x86_64",
15061507
PtxKernel => self.arch == "nvptx64",
15071508
Msp430Interrupt => self.arch == "msp430",

compiler/rustc_typeck/src/check/method/prelude2021.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
156156
segment.ident.name
157157
));
158158

159-
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr);
159+
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr, sp);
160160
if precise {
161161
let args = args
162162
.iter()
163163
.skip(1)
164164
.map(|arg| {
165+
let span = arg.span.find_ancestor_inside(sp).unwrap_or_default();
165166
format!(
166167
", {}",
167-
self.sess().source_map().span_to_snippet(arg.span).unwrap()
168+
self.sess().source_map().span_to_snippet(span).unwrap()
168169
)
169170
})
170171
.collect::<String>();
@@ -173,8 +174,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
173174
sp,
174175
"disambiguate the associated function",
175176
format!(
176-
"{}::{}({}{})",
177-
trait_name, segment.ident.name, self_adjusted, args
177+
"{}::{}{}({}{})",
178+
trait_name,
179+
segment.ident.name,
180+
if let Some(args) = segment.args.as_ref().and_then(|args| self
181+
.sess()
182+
.source_map()
183+
.span_to_snippet(args.span_ext)
184+
.ok())
185+
{
186+
// Keep turbofish.
187+
format!("::{}", args)
188+
} else {
189+
String::new()
190+
},
191+
self_adjusted,
192+
args,
178193
),
179194
Applicability::MachineApplicable,
180195
);
@@ -272,11 +287,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
272287
method_name.name
273288
));
274289

275-
let mut self_ty_name = self
276-
.sess()
277-
.source_map()
278-
.span_to_snippet(self_ty_span)
279-
.unwrap_or_else(|_| self_ty.to_string());
290+
let mut self_ty_name = self_ty_span
291+
.find_ancestor_inside(span)
292+
.and_then(|span| self.sess().source_map().span_to_snippet(span).ok())
293+
.unwrap_or_else(|| self_ty.to_string());
280294

281295
// Get the number of generics the self type has (if an Adt) unless we can determine that
282296
// the user has written the self type with generics already which we (naively) do by looking
@@ -370,7 +384,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
370384
/// Creates a string version of the `expr` that includes explicit adjustments.
371385
/// Returns the string and also a bool indicating whther this is a *precise*
372386
/// suggestion.
373-
fn adjust_expr(&self, pick: &Pick<'tcx>, expr: &hir::Expr<'tcx>) -> (String, bool) {
387+
fn adjust_expr(
388+
&self,
389+
pick: &Pick<'tcx>,
390+
expr: &hir::Expr<'tcx>,
391+
outer: Span,
392+
) -> (String, bool) {
374393
let derefs = "*".repeat(pick.autoderefs);
375394

376395
let autoref = match pick.autoref_or_ptr_adjustment {
@@ -379,12 +398,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379398
Some(probe::AutorefOrPtrAdjustment::ToConstPtr) | None => "",
380399
};
381400

382-
let (expr_text, precise) =
383-
if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) {
384-
(expr_text, true)
385-
} else {
386-
("(..)".to_string(), false)
387-
};
401+
let (expr_text, precise) = if let Some(expr_text) = expr
402+
.span
403+
.find_ancestor_inside(outer)
404+
.and_then(|span| self.sess().source_map().span_to_snippet(span).ok())
405+
{
406+
(expr_text, true)
407+
} else {
408+
("(..)".to_string(), false)
409+
};
388410

389411
let adjusted_text = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) =
390412
pick.autoref_or_ptr_adjustment

compiler/rustc_typeck/src/check/upvar.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680680
migrated_variables_concat
681681
);
682682

683-
let mut closure_body_span = self.tcx.hir().span(body_id.hir_id);
684-
685683
// If the body was entirely expanded from a macro
686684
// invocation, i.e. the body is not contained inside the
687685
// closure span, then we walk up the expansion until we
688686
// find the span before the expansion.
689-
while !closure_body_span.is_dummy() && !closure_span.contains(closure_body_span) {
690-
closure_body_span = closure_body_span.parent().unwrap_or(DUMMY_SP);
691-
}
687+
let closure_body_span = self.tcx.hir().span(body_id.hir_id)
688+
.find_ancestor_inside(closure_span)
689+
.unwrap_or(DUMMY_SP);
692690

693691
if let Ok(s) = self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
694692
let mut lines = s.lines();

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#![feature(const_alloc_layout)]
7777
#![feature(const_arguments_as_str)]
7878
#![feature(const_assert_type)]
79+
#![feature(const_bigint_helper_methods)]
7980
#![feature(const_caller_location)]
8081
#![feature(const_cell_into_inner)]
8182
#![feature(const_discriminant)]

library/core/src/num/int_macros.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,33 @@ macro_rules! int_impl {
13411341
(a as Self, b)
13421342
}
13431343

1344+
/// Calculates `self + rhs + carry` without the ability to overflow.
1345+
///
1346+
/// Performs "ternary addition" which takes in an extra bit to add, and may return an
1347+
/// additional bit of overflow. This allows for chaining together multiple additions
1348+
/// to create "big integers" which represent larger values.
1349+
///
1350+
/// # Examples
1351+
///
1352+
/// Basic usage
1353+
///
1354+
/// ```
1355+
/// #![feature(bigint_helper_methods)]
1356+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, false), (7, false));")]
1357+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, true), (8, false));")]
1358+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), (", stringify!($SelfT), "::MIN, false));")]
1359+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, true), (", stringify!($SelfT), "::MIN + 1, false));")]
1360+
/// ```
1361+
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
1362+
#[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1363+
#[must_use = "this returns the result of the operation, \
1364+
without modifying the original"]
1365+
#[inline]
1366+
pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1367+
let (sum, carry) = (self as $UnsignedT).carrying_add(rhs as $UnsignedT, carry);
1368+
(sum as $SelfT, carry)
1369+
}
1370+
13441371
/// Calculates `self` - `rhs`
13451372
///
13461373
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
@@ -1365,6 +1392,33 @@ macro_rules! int_impl {
13651392
(a as Self, b)
13661393
}
13671394

1395+
/// Calculates `self - rhs - borrow` without the ability to overflow.
1396+
///
1397+
/// Performs "ternary subtraction" which takes in an extra bit to subtract, and may return
1398+
/// an additional bit of overflow. This allows for chaining together multiple subtractions
1399+
/// to create "big integers" which represent larger values.
1400+
///
1401+
/// # Examples
1402+
///
1403+
/// Basic usage
1404+
///
1405+
/// ```
1406+
/// #![feature(bigint_helper_methods)]
1407+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, false), (3, false));")]
1408+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, true), (2, false));")]
1409+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.borrowing_sub(1, false), (", stringify!($SelfT), "::MAX, false));")]
1410+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.borrowing_sub(1, true), (", stringify!($SelfT), "::MAX - 1, false));")]
1411+
/// ```
1412+
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
1413+
#[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1414+
#[must_use = "this returns the result of the operation, \
1415+
without modifying the original"]
1416+
#[inline]
1417+
pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1418+
let (sum, borrow) = (self as $UnsignedT).borrowing_sub(rhs as $UnsignedT, borrow);
1419+
(sum as $SelfT, borrow)
1420+
}
1421+
13681422
/// Calculates the multiplication of `self` and `rhs`.
13691423
///
13701424
/// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow

0 commit comments

Comments
 (0)