Skip to content

Commit 760d14d

Browse files
Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-asm-const, r=lcnr
fix ICE when `asm_const` and `const_refs_to_static` are combined fixes rust-lang#129462 fixes rust-lang#126896 fixes rust-lang#124164 I think this is a case that was missed in the fix for rust-lang#125558, which inserts a type error in the case of an invalid (that is, non-integer) type being passed to an asm `const` operand. I'm not 100% sure that `span_mirbug_and_err` is the right macro here, but it is used earlier with `builtin_deref` and seems to do the trick. r? ```@lcnr```
2 parents f4934ff + 49e3b9a commit 760d14d

10 files changed

+90
-10
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ mod diags {
25222522
}
25232523

25242524
pub(crate) fn emit_errors(&mut self) -> Option<ErrorGuaranteed> {
2525-
let mut res = None;
2525+
let mut res = self.infcx.tainted_by_errors();
25262526

25272527
// Buffer any move errors that we collected and de-duplicated.
25282528
for (_, (_, diag)) in std::mem::take(&mut self.diags.buffered_move_errors) {

compiler/rustc_borrowck/src/universal_regions.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use rustc_macros::extension;
2929
use rustc_middle::ty::fold::TypeFoldable;
3030
use rustc_middle::ty::print::with_no_trimmed_paths;
3131
use rustc_middle::ty::{
32-
self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty, TyCtxt,
32+
self, GenericArgs, GenericArgsRef, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty,
33+
TyCtxt, TypeVisitableExt,
3334
};
3435
use rustc_middle::{bug, span_bug};
3536
use rustc_span::symbol::{kw, sym};
@@ -688,7 +689,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
688689
defining_ty: DefiningTy<'tcx>,
689690
) -> ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>> {
690691
let tcx = self.infcx.tcx;
691-
match defining_ty {
692+
693+
let inputs_and_output = match defining_ty {
692694
DefiningTy::Closure(def_id, args) => {
693695
assert_eq!(self.mir_def.to_def_id(), def_id);
694696
let closure_sig = args.as_closure().sig();
@@ -798,6 +800,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
798800
// "output" (the type of the constant).
799801
assert_eq!(self.mir_def.to_def_id(), def_id);
800802
let ty = tcx.type_of(self.mir_def).instantiate_identity();
803+
801804
let ty = indices.fold_to_region_vids(tcx, ty);
802805
ty::Binder::dummy(tcx.mk_type_list(&[ty]))
803806
}
@@ -807,7 +810,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
807810
let ty = args.as_inline_const().ty();
808811
ty::Binder::dummy(tcx.mk_type_list(&[ty]))
809812
}
813+
};
814+
815+
// FIXME(#129952): We probably want a more principled approach here.
816+
if let Err(terr) = inputs_and_output.skip_binder().error_reported() {
817+
self.infcx.set_tainted_by_errors(terr);
810818
}
819+
820+
inputs_and_output
811821
}
812822
}
813823

tests/ui/asm/const-refs-to-static.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ needs-asm-support
2+
//@ ignore-nvptx64
3+
//@ ignore-spirv
4+
5+
#![feature(const_refs_to_static)]
6+
7+
use std::arch::{asm, global_asm};
8+
use std::ptr::addr_of;
9+
10+
static FOO: u8 = 42;
11+
12+
global_asm!("{}", const addr_of!(FOO));
13+
//~^ ERROR invalid type for `const` operand
14+
15+
#[no_mangle]
16+
fn inline() {
17+
unsafe { asm!("{}", const addr_of!(FOO)) };
18+
//~^ ERROR invalid type for `const` operand
19+
}
20+
21+
fn main() {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: invalid type for `const` operand
2+
--> $DIR/const-refs-to-static.rs:12:19
3+
|
4+
LL | global_asm!("{}", const addr_of!(FOO));
5+
| ^^^^^^-------------
6+
| |
7+
| is a `*const u8`
8+
|
9+
= help: `const` operands must be of an integer type
10+
11+
error: invalid type for `const` operand
12+
--> $DIR/const-refs-to-static.rs:17:25
13+
|
14+
LL | unsafe { asm!("{}", const addr_of!(FOO)) };
15+
| ^^^^^^-------------
16+
| |
17+
| is a `*const u8`
18+
|
19+
= help: `const` operands must be of an integer type
20+
21+
error: aborting due to 2 previous errors
22+

tests/ui/consts/missing_assoc_const_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Range for TwoDigits {
1616

1717
const fn digits(x: u8) -> usize {
1818
match x {
19-
TwoDigits::FIRST..=TwoDigits::LAST => 0,
19+
TwoDigits::FIRST..=TwoDigits::LAST => 0, //~ ERROR: could not evaluate constant pattern
2020
0..=9 | 100..=255 => panic!(),
2121
}
2222
}

tests/ui/consts/missing_assoc_const_type.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: missing type for `const` item
44
LL | const FIRST: = 10;
55
| ^ help: provide a type for the associated constant: `u8`
66

7-
error: aborting due to 1 previous error
7+
error: could not evaluate constant pattern
8+
--> $DIR/missing_assoc_const_type.rs:19:9
9+
|
10+
LL | TwoDigits::FIRST..=TwoDigits::LAST => 0,
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//@ known-bug: #124164
1+
// reported as #124164
22
static S_COUNT: = std::sync::atomic::AtomicUsize::new(0);
3+
//~^ ERROR: missing type for `static` item
34

45
fn main() {}

tests/ui/static/missing-type.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: missing type for `static` item
2+
--> $DIR/missing-type.rs:2:16
3+
|
4+
LL | static S_COUNT: = std::sync::atomic::AtomicUsize::new(0);
5+
| ^ help: provide a type for the static variable: `AtomicUsize`
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//@ known-bug: rust-lang/rust#126896
21
//@ compile-flags: -Zvalidate-mir -Zinline-mir=yes
32

3+
// reported as rust-lang/rust#126896
4+
45
#![feature(type_alias_impl_trait)]
56
type Two<'a, 'b> = impl std::fmt::Debug;
67

@@ -9,9 +10,8 @@ fn set(x: &mut isize) -> isize {
910
}
1011

1112
fn d(x: Two) {
12-
let c1 = || set(x);
13+
let c1 = || set(x); //~ ERROR: expected generic lifetime parameter, found `'_`
1314
c1;
1415
}
1516

16-
fn main() {
17-
}
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0792]: expected generic lifetime parameter, found `'_`
2+
--> $DIR/taint.rs:13:17
3+
|
4+
LL | type Two<'a, 'b> = impl std::fmt::Debug;
5+
| -- this generic parameter must be used with a generic lifetime parameter
6+
...
7+
LL | let c1 = || set(x);
8+
| ^^^^^^
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0792`.

0 commit comments

Comments
 (0)