Skip to content

Commit 784916c

Browse files
committed
Auto merge of #115290 - compiler-errors:ctor-unsafe, r=cjgillot
`rustc_layout_scalar_valid_range` makes ctors unsafe We already validate this when we use the ctor in a call, e.g. `Variant(1)`, but not if we use the ctor as a fn ptr, e.g. `.map(Variant)`. The easiest way to fix the latter is (afaict) is by marking the ctor as unsafe itself. Fixes #115284
2 parents 2bd8131 + bf66723 commit 784916c

21 files changed

+65
-30
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_trait_selection::infer::InferCtxtExt;
3838
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
3939
use rustc_trait_selection::traits::ObligationCtxt;
4040
use std::iter;
41+
use std::ops::Bound;
4142

4243
mod generics_of;
4344
mod item_bounds;
@@ -1144,15 +1145,15 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
11441145
}
11451146

11461147
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
1147-
let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id)).instantiate_identity();
1148+
let adt_def_id = tcx.hir().get_parent_item(hir_id).def_id.to_def_id();
1149+
let ty = tcx.type_of(adt_def_id).instantiate_identity();
11481150
let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id).instantiate_identity());
1149-
ty::Binder::dummy(tcx.mk_fn_sig(
1150-
inputs,
1151-
ty,
1152-
false,
1153-
hir::Unsafety::Normal,
1154-
abi::Abi::Rust,
1155-
))
1151+
// constructors for structs with `layout_scalar_valid_range` are unsafe to call
1152+
let safety = match tcx.layout_scalar_valid_range(adt_def_id) {
1153+
(Bound::Unbounded, Bound::Unbounded) => hir::Unsafety::Normal,
1154+
_ => hir::Unsafety::Unsafe,
1155+
};
1156+
ty::Binder::dummy(tcx.mk_fn_sig(inputs, ty, false, safety, abi::Abi::Rust))
11561157
}
11571158

11581159
Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,10 @@ impl<T> Trait<T> for X {
266266
}
267267
}
268268
}
269-
(ty::FnPtr(_), ty::FnDef(def, _))
270-
if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
271-
diag.note(
272-
"when the arguments and return types match, functions can be coerced \
273-
to function pointers",
274-
);
269+
(ty::FnPtr(sig), ty::FnDef(def_id, _)) | (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
270+
if tcx.fn_sig(*def_id).skip_binder().unsafety() < sig.unsafety() {
271+
diag.note("unsafe functions cannot be coerced into safe function pointers");
272+
}
275273
}
276274
_ => {}
277275
}

tests/ui/argument-suggestions/two-mismatch-notes.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | foo(f, w);
1111
| ^
1212
= note: expected fn pointer `fn(i32)`
1313
found fn item `fn(u32) {f}`
14-
= note: when the arguments and return types match, functions can be coerced to function pointers
1514
note: expected `Wrapper<i32>`, found `Wrapper<isize>`
1615
--> $DIR/two-mismatch-notes.rs:10:12
1716
|

tests/ui/c-variadic/variadic-ffi-1.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
4646
|
4747
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
4848
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
49-
= note: when the arguments and return types match, functions can be coerced to function pointers
5049

5150
error[E0308]: mismatched types
5251
--> $DIR/variadic-ffi-1.rs:26:54
@@ -58,7 +57,6 @@ LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
5857
|
5958
= note: expected fn pointer `extern "C" fn(_, _, ...)`
6059
found fn item `extern "C" fn(_, _) {bar}`
61-
= note: when the arguments and return types match, functions can be coerced to function pointers
6260

6361
error[E0617]: can't pass `f32` to variadic function
6462
--> $DIR/variadic-ffi-1.rs:28:19

tests/ui/fn/fn-pointer-mismatch.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ LL | let e: &fn(u32) -> u32 = &foo;
8080
= note: expected reference `&fn(u32) -> u32`
8181
found reference `&fn(u32) -> u32 {foo}`
8282
= note: fn items are distinct from fn pointers
83-
= note: when the arguments and return types match, functions can be coerced to function pointers
8483
help: consider casting to a fn pointer
8584
|
8685
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);

tests/ui/fn/signature-error-reporting-under-verbose.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ fn main() {
1212
//~| NOTE expected fn pointer, found fn item
1313
//~| NOTE expected fn pointer `fn(i32, u32)`
1414
//~| NOTE arguments to this function are incorrect
15-
//~| NOTE when the arguments and return types match, functions can be coerced to function pointers
1615
}

tests/ui/fn/signature-error-reporting-under-verbose.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | needs_ptr(foo);
88
|
99
= note: expected fn pointer `fn(i32, u32)`
1010
found fn item `fn(i32, i32) {foo}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211
note: function defined here
1312
--> $DIR/signature-error-reporting-under-verbose.rs:5:4
1413
|

tests/ui/issues/issue-10764.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | fn main() { f(bar) }
88
|
99
= note: expected fn pointer `fn()`
1010
found fn item `extern "C" fn() {bar}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211
note: function defined here
1312
--> $DIR/issue-10764.rs:1:4
1413
|

tests/ui/mismatched_types/normalize-fn-sig.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | needs_i32_ref_fn(foo::<()>);
88
|
99
= note: expected fn pointer `fn(&'static i32, i32)`
1010
found fn item `fn(i32, &'static i32) {foo::<()>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211
note: function defined here
1312
--> $DIR/normalize-fn-sig.rs:11:4
1413
|

tests/ui/regions/region-lifetime-bounds-on-fns-where-clause.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
1010
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/regions/region-multiple-lifetime-bounds-on-fns-where-clause.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f> fn(&'a mut &'b isize, &'c mut &'d isize, &'e mut &'f isize)`
1010
found fn item `for<'a, 'b, 'c> fn(&'a mut &isize, &'b mut &isize, &'c mut &isize) {a::<'_, '_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/regions/regions-fn-subtyping-return-static-fail.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | want_G(baz);
88
|
99
= note: expected fn pointer `for<'cx> fn(&'cx S) -> &'static S`
1010
found fn item `for<'a> fn(&'a S) -> &'a S {baz}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211
note: function defined here
1312
--> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
1413
|

tests/ui/regions/regions-lifetime-bounds-on-fns.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
1010
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/reify-intrinsic.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::tr
88
|
99
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
1010
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
1413
--> $DIR/reify-intrinsic.rs:11:13

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
1313
found fn item `fn() {foo}`
1414
= note: fn items are distinct from fn pointers
1515
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
16-
= note: when the arguments and return types match, functions can be coerced to function pointers
1716
help: consider casting to a fn pointer
1817
|
1918
LL | let foo: fn() = foo as fn();

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
1313
found fn item `fn() {foo}`
1414
= note: fn items are distinct from fn pointers
1515
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
16-
= note: when the arguments and return types match, functions can be coerced to function pointers
1716
help: consider casting to a fn pointer
1817
|
1918
LL | let foo: fn() = foo as fn();

tests/ui/static/static-reference-to-fn-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | func: &foo,
77
= note: expected reference `&fn() -> Option<isize>`
88
found reference `&fn() -> Option<isize> {foo}`
99
= note: fn items are distinct from fn pointers
10-
= note: when the arguments and return types match, functions can be coerced to function pointers
1110
help: consider casting to a fn pointer
1211
|
1312
LL | func: &(foo as fn() -> Option<isize>),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(rustc_attrs)]
2+
#![allow(internal_features)]
3+
4+
#[derive(Debug)]
5+
#[rustc_layout_scalar_valid_range_start(2)]
6+
struct NonZeroAndOneU8(u8);
7+
8+
fn main() {
9+
println!("{:?}", Some(1).map(NonZeroAndOneU8).unwrap());
10+
//~^ ERROR found `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
2+
--> $DIR/initializing-ranged-via-ctor.rs:9:34
3+
|
4+
LL | println!("{:?}", Some(1).map(NonZeroAndOneU8).unwrap());
5+
| --- ^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `FnOnce<({integer},)>` is not implemented for fn item `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
10+
= note: unsafe function cannot be called generically without an unsafe block
11+
note: required by a bound in `Option::<T>::map`
12+
--> $SRC_DIR/core/src/option.rs:LL:COL
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[derive(Debug)]
4+
#[rustc_layout_scalar_valid_range_start(2)]
5+
struct NonZeroAndOneU8(u8);
6+
7+
fn main() {
8+
let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
9+
//~^ ERROR mismatched types
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ranged-ctor-as-fn-ptr.rs:8:40
3+
|
4+
LL | let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
5+
| ------------------------- ^^^^^^^^^^^^^^^ expected normal fn, found unsafe fn
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected fn pointer `fn(_) -> NonZeroAndOneU8`
10+
found struct constructor `unsafe fn(_) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
11+
= note: unsafe functions cannot be coerced into safe function pointers
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)