Skip to content

Commit 8fb32ab

Browse files
committed
Auto merge of rust-lang#139473 - Kobzol:rollup-ycksn9b, r=Kobzol
Rollup of 5 pull requests Successful merges: - rust-lang#138314 (fix usage of `autodiff` macro with inner functions) - rust-lang#139426 (Make the UnifyKey and UnifyValue imports non-nightly) - rust-lang#139431 (Remove LLVM 18 inline ASM span fallback) - rust-lang#139456 (style guide: add let-chain rules) - rust-lang#139467 (More trivial tweaks) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b9856b6 + 58ad763 commit 8fb32ab

File tree

12 files changed

+193
-78
lines changed

12 files changed

+193
-78
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4589,6 +4589,7 @@ version = "0.0.0"
45894589
dependencies = [
45904590
"bitflags",
45914591
"derive-where",
4592+
"ena",
45924593
"indexmap",
45934594
"rustc-hash 1.1.0",
45944595
"rustc_ast_ir",

Diff for: compiler/rustc_builtin_macros/src/autodiff.rs

+77-48
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod llvm_enzyme {
1717
use rustc_ast::visit::AssocCtxt::*;
1818
use rustc_ast::{
1919
self as ast, AssocItemKind, BindingMode, ExprKind, FnRetTy, FnSig, Generics, ItemKind,
20-
MetaItemInner, PatKind, QSelf, TyKind,
20+
MetaItemInner, PatKind, QSelf, TyKind, Visibility,
2121
};
2222
use rustc_expand::base::{Annotatable, ExtCtxt};
2323
use rustc_span::{Ident, Span, Symbol, kw, sym};
@@ -72,6 +72,16 @@ mod llvm_enzyme {
7272
}
7373
}
7474

75+
// Get information about the function the macro is applied to
76+
fn extract_item_info(iitem: &P<ast::Item>) -> Option<(Visibility, FnSig, Ident)> {
77+
match &iitem.kind {
78+
ItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
79+
Some((iitem.vis.clone(), sig.clone(), ident.clone()))
80+
}
81+
_ => None,
82+
}
83+
}
84+
7585
pub(crate) fn from_ast(
7686
ecx: &mut ExtCtxt<'_>,
7787
meta_item: &ThinVec<MetaItemInner>,
@@ -199,32 +209,26 @@ mod llvm_enzyme {
199209
return vec![item];
200210
}
201211
let dcx = ecx.sess.dcx();
202-
// first get the annotable item:
203-
let (primal, sig, is_impl): (Ident, FnSig, bool) = match &item {
204-
Annotatable::Item(iitem) => {
205-
let (ident, sig) = match &iitem.kind {
206-
ItemKind::Fn(box ast::Fn { ident, sig, .. }) => (ident, sig),
207-
_ => {
208-
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
209-
return vec![item];
210-
}
211-
};
212-
(*ident, sig.clone(), false)
213-
}
212+
213+
// first get information about the annotable item:
214+
let Some((vis, sig, primal)) = (match &item {
215+
Annotatable::Item(iitem) => extract_item_info(iitem),
216+
Annotatable::Stmt(stmt) => match &stmt.kind {
217+
ast::StmtKind::Item(iitem) => extract_item_info(iitem),
218+
_ => None,
219+
},
214220
Annotatable::AssocItem(assoc_item, Impl { of_trait: false }) => {
215-
let (ident, sig) = match &assoc_item.kind {
216-
ast::AssocItemKind::Fn(box ast::Fn { ident, sig, .. }) => (ident, sig),
217-
_ => {
218-
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
219-
return vec![item];
221+
match &assoc_item.kind {
222+
ast::AssocItemKind::Fn(box ast::Fn { sig, ident, .. }) => {
223+
Some((assoc_item.vis.clone(), sig.clone(), ident.clone()))
220224
}
221-
};
222-
(*ident, sig.clone(), true)
223-
}
224-
_ => {
225-
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
226-
return vec![item];
225+
_ => None,
226+
}
227227
}
228+
_ => None,
229+
}) else {
230+
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
231+
return vec![item];
228232
};
229233

230234
let meta_item_vec: ThinVec<MetaItemInner> = match meta_item.kind {
@@ -238,15 +242,6 @@ mod llvm_enzyme {
238242
let has_ret = has_ret(&sig.decl.output);
239243
let sig_span = ecx.with_call_site_ctxt(sig.span);
240244

241-
let vis = match &item {
242-
Annotatable::Item(iitem) => iitem.vis.clone(),
243-
Annotatable::AssocItem(assoc_item, _) => assoc_item.vis.clone(),
244-
_ => {
245-
dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() });
246-
return vec![item];
247-
}
248-
};
249-
250245
// create TokenStream from vec elemtents:
251246
// meta_item doesn't have a .tokens field
252247
let mut ts: Vec<TokenTree> = vec![];
@@ -379,6 +374,22 @@ mod llvm_enzyme {
379374
}
380375
Annotatable::AssocItem(assoc_item.clone(), i)
381376
}
377+
Annotatable::Stmt(ref mut stmt) => {
378+
match stmt.kind {
379+
ast::StmtKind::Item(ref mut iitem) => {
380+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
381+
iitem.attrs.push(attr);
382+
}
383+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind))
384+
{
385+
iitem.attrs.push(inline_never.clone());
386+
}
387+
}
388+
_ => unreachable!("stmt kind checked previously"),
389+
};
390+
391+
Annotatable::Stmt(stmt.clone())
392+
}
382393
_ => {
383394
unreachable!("annotatable kind checked previously")
384395
}
@@ -389,22 +400,40 @@ mod llvm_enzyme {
389400
delim: rustc_ast::token::Delimiter::Parenthesis,
390401
tokens: ts,
391402
});
403+
392404
let d_attr = outer_normal_attr(&rustc_ad_attr, new_id, span);
393-
let d_annotatable = if is_impl {
394-
let assoc_item: AssocItemKind = ast::AssocItemKind::Fn(asdf);
395-
let d_fn = P(ast::AssocItem {
396-
attrs: thin_vec![d_attr, inline_never],
397-
id: ast::DUMMY_NODE_ID,
398-
span,
399-
vis,
400-
kind: assoc_item,
401-
tokens: None,
402-
});
403-
Annotatable::AssocItem(d_fn, Impl { of_trait: false })
404-
} else {
405-
let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf));
406-
d_fn.vis = vis;
407-
Annotatable::Item(d_fn)
405+
let d_annotatable = match &item {
406+
Annotatable::AssocItem(_, _) => {
407+
let assoc_item: AssocItemKind = ast::AssocItemKind::Fn(asdf);
408+
let d_fn = P(ast::AssocItem {
409+
attrs: thin_vec![d_attr, inline_never],
410+
id: ast::DUMMY_NODE_ID,
411+
span,
412+
vis,
413+
kind: assoc_item,
414+
tokens: None,
415+
});
416+
Annotatable::AssocItem(d_fn, Impl { of_trait: false })
417+
}
418+
Annotatable::Item(_) => {
419+
let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf));
420+
d_fn.vis = vis;
421+
422+
Annotatable::Item(d_fn)
423+
}
424+
Annotatable::Stmt(_) => {
425+
let mut d_fn = ecx.item(span, thin_vec![d_attr, inline_never], ItemKind::Fn(asdf));
426+
d_fn.vis = vis;
427+
428+
Annotatable::Stmt(P(ast::Stmt {
429+
id: ast::DUMMY_NODE_ID,
430+
kind: ast::StmtKind::Item(d_fn),
431+
span,
432+
}))
433+
}
434+
_ => {
435+
unreachable!("item kind checked previously")
436+
}
408437
};
409438

410439
return vec![orig_annotatable, d_annotatable];

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,9 @@ fn report_inline_asm(
439439
let span = if cookie == 0 || matches!(cgcx.lto, Lto::Fat | Lto::Thin) {
440440
SpanData::default()
441441
} else {
442-
let lo = BytePos::from_u32(cookie as u32);
443-
let hi = BytePos::from_u32((cookie >> 32) as u32);
444442
SpanData {
445-
lo,
446-
// LLVM version < 19 silently truncates the cookie to 32 bits in some situations.
447-
hi: if hi.to_u32() != 0 { hi } else { lo },
443+
lo: BytePos::from_u32(cookie as u32),
444+
hi: BytePos::from_u32((cookie >> 32) as u32),
448445
ctxt: SyntaxContext::root(),
449446
parent: None,
450447
}

Diff for: compiler/rustc_hir_analysis/src/check/region.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,11 @@ fn resolve_expr<'tcx>(
446446
// Mark this expr's scope and all parent scopes as containing `yield`.
447447
let mut scope = Scope { local_id: expr.hir_id.local_id, data: ScopeData::Node };
448448
loop {
449-
let span = match expr.kind {
450-
hir::ExprKind::Yield(expr, hir::YieldSource::Await { .. }) => {
451-
expr.span.shrink_to_hi().to(expr.span)
452-
}
453-
_ => expr.span,
449+
let data = YieldData {
450+
span: expr.span,
451+
expr_and_pat_count: visitor.expr_and_pat_count,
452+
source: *source,
454453
};
455-
let data =
456-
YieldData { span, expr_and_pat_count: visitor.expr_and_pat_count, source: *source };
457454
match visitor.scope_tree.yield_in_scope.get_mut(&scope) {
458455
Some(yields) => yields.push(data),
459456
None => {

Diff for: compiler/rustc_hir_typeck/src/demand.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
865865
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
866866
vec![(
867867
ty_ref.1.ty.span.shrink_to_lo(),
868-
format!(
869-
"{}mut ",
870-
if ty_ref.0.ident.span.lo() == ty_ref.0.ident.span.hi() { "" } else { " " },
871-
),
868+
format!("{}mut ", if ty_ref.0.ident.span.is_empty() { "" } else { " " },),
872869
)]
873870
};
874871
sugg.extend([

Diff for: compiler/rustc_passes/src/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ impl<'tcx> Liveness<'_, 'tcx> {
16551655
// `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
16561656
Some(mut_ty.ty.span.shrink_to_lo())
16571657
};
1658-
let pre = if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " };
1658+
let pre = if lt.ident.span.is_empty() { "" } else { " " };
16591659
Some(errors::UnusedAssignSuggestion {
16601660
ty_span,
16611661
pre,

Diff for: compiler/rustc_type_ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
# tidy-alphabetical-start
88
bitflags = "2.4.1"
99
derive-where = "1.2.7"
10+
ena = "0.14.3"
1011
indexmap = "2.0.0"
1112
rustc-hash = "1.1.0"
1213
rustc_ast_ir = { path = "../rustc_ast_ir", default-features = false }

Diff for: compiler/rustc_type_ir/src/data_structures/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::hash::BuildHasherDefault;
22

3+
pub use ena::unify::{NoError, UnifyKey, UnifyValue};
34
use rustc_hash::FxHasher;
45
pub use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
56

Diff for: compiler/rustc_type_ir/src/ty_kind.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use rustc_ast_ir::Mutability;
66
#[cfg(feature = "nightly")]
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
#[cfg(feature = "nightly")]
9-
use rustc_data_structures::unify::{NoError, UnifyKey, UnifyValue};
10-
#[cfg(feature = "nightly")]
119
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
10+
use rustc_type_ir::data_structures::{NoError, UnifyKey, UnifyValue};
1211
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
1312

1413
use self::TyKind::*;
@@ -796,7 +795,6 @@ pub enum InferTy {
796795

797796
/// Raw `TyVid` are used as the unification key for `sub_relations`;
798797
/// they carry no values.
799-
#[cfg(feature = "nightly")]
800798
impl UnifyKey for TyVid {
801799
type Value = ();
802800
#[inline]
@@ -812,7 +810,6 @@ impl UnifyKey for TyVid {
812810
}
813811
}
814812

815-
#[cfg(feature = "nightly")]
816813
impl UnifyValue for IntVarValue {
817814
type Error = NoError;
818815

@@ -832,7 +829,6 @@ impl UnifyValue for IntVarValue {
832829
}
833830
}
834831

835-
#[cfg(feature = "nightly")]
836832
impl UnifyKey for IntVid {
837833
type Value = IntVarValue;
838834
#[inline] // make this function eligible for inlining - it is quite hot.
@@ -848,7 +844,6 @@ impl UnifyKey for IntVid {
848844
}
849845
}
850846

851-
#[cfg(feature = "nightly")]
852847
impl UnifyValue for FloatVarValue {
853848
type Error = NoError;
854849

@@ -866,7 +861,6 @@ impl UnifyValue for FloatVarValue {
866861
}
867862
}
868863

869-
#[cfg(feature = "nightly")]
870864
impl UnifyKey for FloatVid {
871865
type Value = FloatVarValue;
872866
#[inline]

Diff for: src/doc/style-guide/src/expressions.md

+72-6
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,11 @@ self.pre_comment.as_ref().map_or(
521521

522522
## Control flow expressions
523523

524-
This section covers `if`, `if let`, `loop`, `while`, `while let`, and `for`
525-
expressions.
524+
This section covers `for` and `loop` expressions, as well as `if` and `while`
525+
expressions with their sub-expression variants. This includes those with a
526+
single `let` sub-expression (i.e. `if let` and `while let`)
527+
as well as "let-chains": those with one or more `let` sub-expressions and
528+
one or more bool-type conditions (i.e. `if a && let Some(b) = c`).
526529

527530
Put the keyword, any initial clauses, and the opening brace of the block all on
528531
a single line, if they fit. Apply the usual rules for [block
@@ -548,10 +551,11 @@ if let ... {
548551
}
549552
```
550553

551-
If the control line needs to be broken, prefer to break before the `=` in `*
552-
let` expressions and before `in` in a `for` expression; block-indent the
553-
following line. If the control line is broken for any reason, put the opening
554-
brace on its own line, not indented. Examples:
554+
If the control line needs to be broken, then prefer breaking after the `=` for any
555+
`let` sub-expression in an `if` or `while` expression that does not fit,
556+
and before `in` in a `for` expression; the following line should be block indented.
557+
If the control line is broken for any reason, then the opening brace should be on its
558+
own line and not indented. Examples:
555559

556560
```rust
557561
while let Some(foo)
@@ -572,6 +576,68 @@ if a_long_expression
572576
{
573577
...
574578
}
579+
580+
if let Some(a) = b
581+
&& another_long_expression
582+
&& a_third_long_expression
583+
{
584+
// ...
585+
}
586+
587+
if let Some(relatively_long_thing)
588+
= a_long_expression
589+
&& another_long_expression
590+
&& a_third_long_expression
591+
{
592+
// ...
593+
}
594+
595+
if some_expr
596+
&& another_long_expression
597+
&& let Some(relatively_long_thing) =
598+
a_long_long_long_long_long_long_really_reallllllllllyyyyyyy_long_expression
599+
&& a_third_long_expression
600+
{
601+
// ...
602+
}
603+
```
604+
605+
A let-chain control line is allowed to be formatted on a single line provided
606+
it only consists of two clauses, with the first, left-hand side operand being a literal or an
607+
`ident` (which can optionally be preceded by any number of unary prefix operators),
608+
and the second, right-hand side operand being a single-line `let` clause. Otherwise,
609+
the control line must be broken and formatted according to the above rules. For example:
610+
611+
```rust
612+
if a && let Some(b) = foo() {
613+
// ...
614+
}
615+
616+
if true && let Some(b) = foo() {
617+
// ...
618+
}
619+
620+
let operator = if !from_hir_call && let Some(p) = parent {
621+
// ...
622+
};
623+
624+
if let Some(b) = foo()
625+
&& a
626+
{
627+
// ..
628+
}
629+
630+
if foo()
631+
&& let Some(b) = bar
632+
{
633+
// ...
634+
}
635+
636+
if gen_pos != GenericArgPosition::Type
637+
&& let Some(b) = gen_args.bindings.first()
638+
{
639+
// ..
640+
}
575641
```
576642

577643
Where the initial clause spans multiple lines and ends with one or more closing

0 commit comments

Comments
 (0)