Skip to content

Commit cf3b561

Browse files
committed
Gate use of raw and function pointers in const generics behind
const_compare_raw_pointers.
1 parent a59eb6d commit cf3b561

11 files changed

+131
-7
lines changed

src/librustc_typeck/collect.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,9 +1508,29 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
15081508
}
15091509

15101510
Node::GenericParam(param) => match &param.kind {
1511-
hir::GenericParamKind::Type { default: Some(ref ty), .. } |
1512-
hir::GenericParamKind::Const { ref ty, .. } => {
1513-
icx.to_ty(ty)
1511+
hir::GenericParamKind::Type { default: Some(ref ty), .. } => icx.to_ty(ty),
1512+
hir::GenericParamKind::Const { ty: ref hir_ty, .. } => {
1513+
let ty = icx.to_ty(hir_ty);
1514+
if !tcx.features().const_compare_raw_pointers {
1515+
let err = match ty.peel_refs().kind {
1516+
ty::FnPtr(_) => Some("function pointers"),
1517+
ty::RawPtr(_) => Some("raw pointers"),
1518+
_ => None,
1519+
};
1520+
if let Some(unsupported_type) = err {
1521+
feature_gate::emit_feature_err(
1522+
&tcx.sess.parse_sess,
1523+
sym::const_compare_raw_pointers,
1524+
hir_ty.span,
1525+
feature_gate::GateIssue::Language,
1526+
&format!(
1527+
"use of {} as const generic arguments are unstable",
1528+
unsupported_type
1529+
),
1530+
);
1531+
};
1532+
}
1533+
ty
15141534
}
15151535
x => {
15161536
if !fail {

src/test/ui/const-generics/fn-const-param-call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-pass
22

3-
#![feature(const_generics)]
3+
#![feature(const_generics, const_compare_raw_pointers)]
44
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
55

66
fn function() -> u32 {

src/test/ui/const-generics/fn-const-param-call.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
22
--> $DIR/fn-const-param-call.rs:3:12
33
|
4-
LL | #![feature(const_generics)]
4+
LL | #![feature(const_generics, const_compare_raw_pointers)]
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(incomplete_features)]` on by default

src/test/ui/const-generics/fn-const-param-infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(const_generics)]
1+
#![feature(const_generics, const_compare_raw_pointers)]
22
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
33

44
struct Checked<const F: fn(usize) -> bool>;

src/test/ui/const-generics/fn-const-param-infer.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
22
--> $DIR/fn-const-param-infer.rs:1:12
33
|
4-
LL | #![feature(const_generics)]
4+
LL | #![feature(const_generics, const_compare_raw_pointers)]
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(incomplete_features)]` on by default
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-pass
2+
#![feature(const_generics, const_compare_raw_pointers)]
3+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
4+
5+
const A: u32 = 3;
6+
7+
struct Const<const P: *const u32>;
8+
9+
impl<const P: *const u32> Const<{P}> {
10+
fn get() -> u32 {
11+
unsafe {
12+
*P
13+
}
14+
}
15+
}
16+
17+
fn main() {
18+
assert_eq!(Const::<{&A as *const _}>::get(), 3)
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/raw-ptr-const-param-deref.rs:2:12
3+
|
4+
LL | #![feature(const_generics, const_compare_raw_pointers)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(const_generics, const_compare_raw_pointers)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
struct Const<const P: *const u32>;
5+
6+
fn main() {
7+
let _: Const<{15 as *const _}> = Const::<{10 as *const _}>; //~ mismatched types
8+
let _: Const<{10 as *const _}> = Const::<{10 as *const _}>;
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/raw-ptr-const-param.rs:1:12
3+
|
4+
LL | #![feature(const_generics, const_compare_raw_pointers)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/raw-ptr-const-param.rs:7:38
11+
|
12+
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Scalar(0x000000000000000f) : *const u32`, found `Scalar(0x000000000000000a) : *const u32`
14+
|
15+
= note: expected type `Const<>`
16+
found type `Const<>`
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct ConstFn<const F: fn()>;
2+
//~^ ERROR const generics are unstable
3+
//~^^ ERROR use of function pointers as const generic arguments are unstable
4+
5+
struct ConstPtr<const P: *const u32>;
6+
//~^ ERROR const generics are unstable
7+
//~^^ ERROR use of raw pointers as const generic arguments are unstable
8+
9+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: const generics are unstable
2+
--> $DIR/feature-gate-const_generics-ptr.rs:1:22
3+
|
4+
LL | struct ConstFn<const F: fn()>;
5+
| ^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
8+
= help: add `#![feature(const_generics)]` to the crate attributes to enable
9+
10+
error[E0658]: const generics are unstable
11+
--> $DIR/feature-gate-const_generics-ptr.rs:5:23
12+
|
13+
LL | struct ConstPtr<const P: *const u32>;
14+
| ^
15+
|
16+
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
17+
= help: add `#![feature(const_generics)]` to the crate attributes to enable
18+
19+
error[E0658]: use of function pointers as const generic arguments are unstable
20+
--> $DIR/feature-gate-const_generics-ptr.rs:1:25
21+
|
22+
LL | struct ConstFn<const F: fn()>;
23+
| ^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/53020
26+
= help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable
27+
28+
error[E0658]: use of raw pointers as const generic arguments are unstable
29+
--> $DIR/feature-gate-const_generics-ptr.rs:5:26
30+
|
31+
LL | struct ConstPtr<const P: *const u32>;
32+
| ^^^^^^^^^^
33+
|
34+
= note: for more information, see https://github.com/rust-lang/rust/issues/53020
35+
= help: add `#![feature(const_compare_raw_pointers)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)