Skip to content

Commit bca37a2

Browse files
Auto merge of #150798 - matthiaskrgr:rollup-ANxstUE, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #150675 (MGCA: Support tuple expressions as direct const arguments) - #150696 (enrich error info when tries to dlopen Enzyme) - #150747 (tests/ui/runtime/on-broken-pipe/with-rustc_main.rs: Not needed so remove) - #150757 (Fix `alloc_error_handler` signature mismatch) - #150777 (Stabilize `slice::element_offset`) - #150791 (Remove out of date FIXME comment.) r? @ghost
2 parents 598c7bd + bfec56b commit bca37a2

File tree

46 files changed

+437
-177
lines changed

Some content is hidden

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

46 files changed

+437
-177
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
312312

313313
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir, AmbigArg>) {
314314
self.insert(
315-
const_arg.as_unambig_ct().span(),
315+
const_arg.as_unambig_ct().span,
316316
const_arg.hir_id,
317317
Node::ConstArg(const_arg.as_unambig_ct()),
318318
);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,8 +2285,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22852285
// `ExprKind::Paren(ExprKind::Underscore)` and should also be lowered to `GenericArg::Infer`
22862286
match c.value.peel_parens().kind {
22872287
ExprKind::Underscore => {
2288-
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
2289-
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
2288+
let ct_kind = hir::ConstArgKind::Infer(());
2289+
self.arena.alloc(hir::ConstArg {
2290+
hir_id: self.lower_node_id(c.id),
2291+
kind: ct_kind,
2292+
span: self.lower_span(c.value.span),
2293+
})
22902294
}
22912295
_ => self.lower_anon_const_to_const_arg_and_alloc(c),
22922296
}
@@ -2356,7 +2360,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23562360
hir::ConstArgKind::Anon(ct)
23572361
};
23582362

2359-
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
2363+
self.arena.alloc(hir::ConstArg {
2364+
hir_id: self.next_id(),
2365+
kind: ct_kind,
2366+
span: self.lower_span(span),
2367+
})
23602368
}
23612369

23622370
fn lower_const_item_rhs(
@@ -2373,9 +2381,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23732381
let const_arg = ConstArg {
23742382
hir_id: self.next_id(),
23752383
kind: hir::ConstArgKind::Error(
2376-
DUMMY_SP,
23772384
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
23782385
),
2386+
span: DUMMY_SP,
23792387
};
23802388
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
23812389
}
@@ -2388,13 +2396,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23882396

23892397
#[instrument(level = "debug", skip(self), ret)]
23902398
fn lower_expr_to_const_arg_direct(&mut self, expr: &Expr) -> hir::ConstArg<'hir> {
2399+
let span = self.lower_span(expr.span);
2400+
23912401
let overly_complex_const = |this: &mut Self| {
23922402
let e = this.dcx().struct_span_err(
23932403
expr.span,
23942404
"complex const arguments must be placed inside of a `const` block",
23952405
);
23962406

2397-
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(expr.span, e.emit()) }
2407+
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(e.emit()), span }
23982408
};
23992409

24002410
match &expr.kind {
@@ -2425,8 +2435,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24252435
ConstArg {
24262436
hir_id: self.next_id(),
24272437
kind: hir::ConstArgKind::TupleCall(qpath, lowered_args),
2438+
span,
24282439
}
24292440
}
2441+
ExprKind::Tup(exprs) => {
2442+
let exprs = self.arena.alloc_from_iter(exprs.iter().map(|expr| {
2443+
let expr = if let ExprKind::ConstBlock(anon_const) = &expr.kind {
2444+
let def_id = self.local_def_id(anon_const.id);
2445+
let def_kind = self.tcx.def_kind(def_id);
2446+
assert_eq!(DefKind::AnonConst, def_kind);
2447+
2448+
self.lower_anon_const_to_const_arg(anon_const)
2449+
} else {
2450+
self.lower_expr_to_const_arg_direct(&expr)
2451+
};
2452+
2453+
&*self.arena.alloc(expr)
2454+
}));
2455+
2456+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(exprs), span }
2457+
}
24302458
ExprKind::Path(qself, path) => {
24312459
let qpath = self.lower_qpath(
24322460
expr.id,
@@ -2439,7 +2467,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24392467
None,
24402468
);
24412469

2442-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
2470+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath), span }
24432471
}
24442472
ExprKind::Struct(se) => {
24452473
let path = self.lower_qpath(
@@ -2480,11 +2508,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24802508
})
24812509
}));
24822510

2483-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2511+
ConstArg {
2512+
hir_id: self.next_id(),
2513+
kind: hir::ConstArgKind::Struct(path, fields),
2514+
span,
2515+
}
24842516
}
24852517
ExprKind::Underscore => ConstArg {
24862518
hir_id: self.lower_node_id(expr.id),
2487-
kind: hir::ConstArgKind::Infer(expr.span, ()),
2519+
kind: hir::ConstArgKind::Infer(()),
2520+
span,
24882521
},
24892522
ExprKind::Block(block, _) => {
24902523
if let [stmt] = block.stmts.as_slice()
@@ -2495,6 +2528,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24952528
| ExprKind::Path(..)
24962529
| ExprKind::Struct(..)
24972530
| ExprKind::Call(..)
2531+
| ExprKind::Tup(..)
24982532
)
24992533
{
25002534
return self.lower_expr_to_const_arg_direct(expr);
@@ -2528,7 +2562,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25282562
return match anon.mgca_disambiguation {
25292563
MgcaDisambiguation::AnonConst => {
25302564
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2531-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2565+
ConstArg {
2566+
hir_id: self.next_id(),
2567+
kind: hir::ConstArgKind::Anon(lowered_anon),
2568+
span: lowered_anon.span,
2569+
}
25322570
}
25332571
MgcaDisambiguation::Direct => self.lower_expr_to_const_arg_direct(&anon.value),
25342572
};
@@ -2565,11 +2603,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25652603
return ConstArg {
25662604
hir_id: self.lower_node_id(anon.id),
25672605
kind: hir::ConstArgKind::Path(qpath),
2606+
span: self.lower_span(expr.span),
25682607
};
25692608
}
25702609

25712610
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2572-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2611+
ConstArg {
2612+
hir_id: self.next_id(),
2613+
kind: hir::ConstArgKind::Anon(lowered_anon),
2614+
span: self.lower_span(expr.span),
2615+
}
25732616
}
25742617

25752618
/// See [`hir::ConstArg`] for when to use this function vs

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
513513
self.arena.alloc(hir::ConstArg {
514514
hir_id: self.next_id(),
515515
kind: hir::ConstArgKind::Anon(self.arena.alloc(anon_const)),
516+
span,
516517
})
517518
}
518519

@@ -557,6 +558,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
557558
})
558559
});
559560
let hir_id = self.next_id();
560-
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id })
561+
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id, span })
561562
}
562563
}

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend. Did you install it via rustup?
1+
codegen_llvm_autodiff_component_missing = autodiff backend not found in the sysroot: {$err}
2+
.note = it will be distributed via rustup in the future
3+
4+
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend: {$err}
25
36
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
47
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3434

3535
#[derive(Diagnostic)]
3636
#[diag(codegen_llvm_autodiff_component_unavailable)]
37-
pub(crate) struct AutoDiffComponentUnavailable;
37+
pub(crate) struct AutoDiffComponentUnavailable {
38+
pub err: String,
39+
}
40+
41+
#[derive(Diagnostic)]
42+
#[diag(codegen_llvm_autodiff_component_missing)]
43+
#[note]
44+
pub(crate) struct AutoDiffComponentMissing {
45+
pub err: String,
46+
}
3847

3948
#[derive(Diagnostic)]
4049
#[diag(codegen_llvm_autodiff_without_lto)]

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,14 @@ impl CodegenBackend for LlvmCodegenBackend {
249249

250250
use crate::back::lto::enable_autodiff_settings;
251251
if sess.opts.unstable_opts.autodiff.contains(&AutoDiff::Enable) {
252-
if let Err(_) = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
253-
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
252+
match llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
253+
Ok(_) => {}
254+
Err(llvm::EnzymeLibraryError::NotFound { err }) => {
255+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentMissing { err });
256+
}
257+
Err(llvm::EnzymeLibraryError::LoadFailed { err }) => {
258+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable { err });
259+
}
254260
}
255261
enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
256262
}

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(crate) mod Enzyme_AD {
153153
fn load_ptr_by_symbol_mut_void(
154154
lib: &libloading::Library,
155155
bytes: &[u8],
156-
) -> Result<*mut c_void, Box<dyn std::error::Error>> {
156+
) -> Result<*mut c_void, libloading::Error> {
157157
unsafe {
158158
let s: libloading::Symbol<'_, *mut c_void> = lib.get(bytes)?;
159159
// libloading = 0.9.0: try_as_raw_ptr always succeeds and returns Some
@@ -192,15 +192,27 @@ pub(crate) mod Enzyme_AD {
192192

193193
static ENZYME_INSTANCE: OnceLock<Mutex<EnzymeWrapper>> = OnceLock::new();
194194

195+
#[derive(Debug)]
196+
pub(crate) enum EnzymeLibraryError {
197+
NotFound { err: String },
198+
LoadFailed { err: String },
199+
}
200+
201+
impl From<libloading::Error> for EnzymeLibraryError {
202+
fn from(err: libloading::Error) -> Self {
203+
Self::LoadFailed { err: format!("{err:?}") }
204+
}
205+
}
206+
195207
impl EnzymeWrapper {
196208
/// Initialize EnzymeWrapper with the given sysroot if not already initialized.
197209
/// Safe to call multiple times - subsequent calls are no-ops due to OnceLock.
198210
pub(crate) fn get_or_init(
199211
sysroot: &rustc_session::config::Sysroot,
200-
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
212+
) -> Result<MutexGuard<'static, Self>, EnzymeLibraryError> {
201213
let mtx: &'static Mutex<EnzymeWrapper> = ENZYME_INSTANCE.get_or_try_init(|| {
202214
let w = Self::call_dynamic(sysroot)?;
203-
Ok::<_, Box<dyn std::error::Error>>(Mutex::new(w))
215+
Ok::<_, EnzymeLibraryError>(Mutex::new(w))
204216
})?;
205217

206218
Ok(mtx.lock().unwrap())
@@ -351,7 +363,7 @@ pub(crate) mod Enzyme_AD {
351363
#[allow(non_snake_case)]
352364
fn call_dynamic(
353365
sysroot: &rustc_session::config::Sysroot,
354-
) -> Result<Self, Box<dyn std::error::Error>> {
366+
) -> Result<Self, EnzymeLibraryError> {
355367
let enzyme_path = Self::get_enzyme_path(sysroot)?;
356368
let lib = unsafe { libloading::Library::new(enzyme_path)? };
357369

@@ -416,7 +428,7 @@ pub(crate) mod Enzyme_AD {
416428
})
417429
}
418430

419-
fn get_enzyme_path(sysroot: &Sysroot) -> Result<String, String> {
431+
fn get_enzyme_path(sysroot: &Sysroot) -> Result<String, EnzymeLibraryError> {
420432
let llvm_version_major = unsafe { LLVMRustVersionMajor() };
421433

422434
let path_buf = sysroot
@@ -434,15 +446,19 @@ pub(crate) mod Enzyme_AD {
434446
.map(|p| p.join("lib").display().to_string())
435447
.collect::<Vec<String>>()
436448
.join("\n* ");
437-
format!(
438-
"failed to find a `libEnzyme-{llvm_version_major}` folder \
449+
EnzymeLibraryError::NotFound {
450+
err: format!(
451+
"failed to find a `libEnzyme-{llvm_version_major}` folder \
439452
in the sysroot candidates:\n* {candidates}"
440-
)
453+
),
454+
}
441455
})?;
442456

443457
Ok(path_buf
444458
.to_str()
445-
.ok_or_else(|| format!("invalid UTF-8 in path: {}", path_buf.display()))?
459+
.ok_or_else(|| EnzymeLibraryError::LoadFailed {
460+
err: format!("invalid UTF-8 in path: {}", path_buf.display()),
461+
})?
446462
.to_string())
447463
}
448464
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::time::{Duration, Instant};
66
use itertools::Itertools;
77
use rustc_abi::FIRST_VARIANT;
88
use rustc_ast::expand::allocator::{
9-
ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, AllocatorKind, AllocatorMethod, AllocatorTy,
9+
ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, AllocatorKind, AllocatorMethod, AllocatorMethodInput,
10+
AllocatorTy,
1011
};
1112
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1213
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
@@ -671,7 +672,7 @@ pub fn allocator_shim_contents(tcx: TyCtxt<'_>, kind: AllocatorKind) -> Vec<Allo
671672
methods.push(AllocatorMethod {
672673
name: ALLOC_ERROR_HANDLER,
673674
special: None,
674-
inputs: &[],
675+
inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
675676
output: AllocatorTy::Never,
676677
});
677678
}

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'hir> ConstItemRhs<'hir> {
423423
pub fn span<'tcx>(&self, tcx: impl crate::intravisit::HirTyCtxt<'tcx>) -> Span {
424424
match self {
425425
ConstItemRhs::Body(body_id) => tcx.hir_body(*body_id).value.span,
426-
ConstItemRhs::TypeConst(ct_arg) => ct_arg.span(),
426+
ConstItemRhs::TypeConst(ct_arg) => ct_arg.span,
427427
}
428428
}
429429
}
@@ -447,6 +447,7 @@ pub struct ConstArg<'hir, Unambig = ()> {
447447
#[stable_hasher(ignore)]
448448
pub hir_id: HirId,
449449
pub kind: ConstArgKind<'hir, Unambig>,
450+
pub span: Span,
450451
}
451452

452453
impl<'hir> ConstArg<'hir, AmbigArg> {
@@ -475,7 +476,7 @@ impl<'hir> ConstArg<'hir> {
475476
/// Functions accepting ambiguous consts will not handle the [`ConstArgKind::Infer`] variant, if
476477
/// infer consts are relevant to you then care should be taken to handle them separately.
477478
pub fn try_as_ambig_ct(&self) -> Option<&ConstArg<'hir, AmbigArg>> {
478-
if let ConstArgKind::Infer(_, ()) = self.kind {
479+
if let ConstArgKind::Infer(()) = self.kind {
479480
return None;
480481
}
481482

@@ -494,23 +495,13 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
494495
_ => None,
495496
}
496497
}
497-
498-
pub fn span(&self) -> Span {
499-
match self.kind {
500-
ConstArgKind::Struct(path, _) => path.span(),
501-
ConstArgKind::Path(path) => path.span(),
502-
ConstArgKind::TupleCall(path, _) => path.span(),
503-
ConstArgKind::Anon(anon) => anon.span,
504-
ConstArgKind::Error(span, _) => span,
505-
ConstArgKind::Infer(span, _) => span,
506-
}
507-
}
508498
}
509499

510500
/// See [`ConstArg`].
511501
#[derive(Clone, Copy, Debug, HashStable_Generic)]
512502
#[repr(u8, C)]
513503
pub enum ConstArgKind<'hir, Unambig = ()> {
504+
Tup(&'hir [&'hir ConstArg<'hir, Unambig>]),
514505
/// **Note:** Currently this is only used for bare const params
515506
/// (`N` where `fn foo<const N: usize>(...)`),
516507
/// not paths to any const (`N` where `const N: usize = ...`).
@@ -523,10 +514,10 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
523514
/// Tuple constructor variant
524515
TupleCall(QPath<'hir>, &'hir [&'hir ConstArg<'hir>]),
525516
/// Error const
526-
Error(Span, ErrorGuaranteed),
517+
Error(ErrorGuaranteed),
527518
/// This variant is not always used to represent inference consts, sometimes
528519
/// [`GenericArg::Infer`] is used instead.
529-
Infer(Span, Unambig),
520+
Infer(Unambig),
530521
}
531522

532523
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -572,7 +563,7 @@ impl GenericArg<'_> {
572563
match self {
573564
GenericArg::Lifetime(l) => l.ident.span,
574565
GenericArg::Type(t) => t.span,
575-
GenericArg::Const(c) => c.span(),
566+
GenericArg::Const(c) => c.span,
576567
GenericArg::Infer(i) => i.span,
577568
}
578569
}

0 commit comments

Comments
 (0)