Skip to content

Commit d98f42b

Browse files
committed
Do not allow extern unsized_fn_param in generic args
1 parent a04fe4d commit d98f42b

8 files changed

+77
-1
lines changed

Diff for: compiler/rustc_monomorphize/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ monomorphize_type_length_limit = reached the type-length limit while instantiati
3030
monomorphize_unknown_cgu_collection_mode =
3131
unknown codegen-item collection mode '{$mode}', falling back to 'lazy' mode
3232
33+
monomorphize_unsized_extern_fn_param = unsized arguments must not be `extern` types
3334
monomorphize_unused_generic_params = item has unused generic parameters
3435
3536
monomorphize_written_to_path = the full type name has been written to '{$path}'

Diff for: compiler/rustc_monomorphize/src/collector.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,12 @@ fn visit_fn_use<'tcx>(
10411041
_ => bug!("failed to resolve instance for {ty}"),
10421042
}
10431043
};
1044+
for &param_ty in ty.fn_sig(tcx).inputs().skip_binder() {
1045+
let tail = tcx.struct_tail_with_normalize(param_ty, |ty| ty, || {});
1046+
if matches!(tail.kind(), ty::Foreign(..)) {
1047+
tcx.dcx().emit_fatal(errors::UnsizedExternParam { span: source });
1048+
}
1049+
}
10441050
visit_instance_use(tcx, instance, is_direct_call, source, output);
10451051
}
10461052
}

Diff for: compiler/rustc_monomorphize/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,10 @@ pub struct StartNotFound;
104104
pub struct UnknownCguCollectionMode<'a> {
105105
pub mode: &'a str,
106106
}
107+
108+
#[derive(Diagnostic)]
109+
#[diag(monomorphize_unsized_extern_fn_param)]
110+
pub struct UnsizedExternParam {
111+
#[primary_span]
112+
pub span: Span,
113+
}

Diff for: tests/ui/recursion/recursion.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
error: reached the recursion limit finding the struct tail for `Nil`
2+
|
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]`
4+
5+
error: reached the recursion limit finding the struct tail for `Nil`
6+
|
7+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]`
8+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
9+
10+
note: the above error was encountered while instantiating `fn test::<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Cons<Nil>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
11+
--> $DIR/recursion.rs:18:11
12+
|
13+
LL | _ => {test (n-1, i+1, Cons {head:2*i+1, tail:first}, Cons{head:i*i, tail:second})}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
116
error: reached the recursion limit while instantiating `test::<Cons<Cons<Cons<Cons<Cons<...>>>>>>`
217
--> $DIR/recursion.rs:18:11
318
|
@@ -11,5 +26,5 @@ LL | fn test<T:Dot> (n:isize, i:isize, first:T, second:T) ->isize {
1126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1227
= note: the full type name has been written to '$TEST_BUILD_DIR/recursion/recursion/recursion.long-type.txt'
1328

14-
error: aborting due to 1 previous error
29+
error: aborting due to 3 previous errors
1530

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://github.com/rust-lang/rust/issues/123887
2+
// Do not ICE on unsized extern parameter
3+
//@ compile-flags: -Clink-dead-code --emit=link
4+
#![feature(extern_types, unsized_fn_params)]
5+
6+
extern "C" {
7+
type ExternType;
8+
}
9+
10+
struct Wrapper<T: ?Sized>(T);
11+
fn f(_: impl ?Sized) {}
12+
fn g(x: Box<Wrapper<ExternType>>) {
13+
f(*x); //~ ERROR unsized arguments must not be `extern` types
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unsized arguments must not be `extern` types
2+
--> $DIR/extern-parameter-issue-123887-generic-wrapped.rs:13:5
3+
|
4+
LL | f(*x);
5+
| ^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://github.com/rust-lang/rust/issues/123887
2+
// Do not ICE on unsized extern parameter
3+
//@ compile-flags: -Clink-dead-code --emit=link
4+
#![feature(extern_types, unsized_fn_params)]
5+
6+
extern "C" {
7+
type ExternType;
8+
}
9+
10+
fn f(_: impl ?Sized) {}
11+
fn g(x: Box<ExternType>) {
12+
f(*x); //~ ERROR unsized arguments must not be `extern` types
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unsized arguments must not be `extern` types
2+
--> $DIR/extern-parameter-issue-123887-generic.rs:12:5
3+
|
4+
LL | f(*x);
5+
| ^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)