Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow extern unsized_fn_param #123894

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ pub(super) fn check_fn<'a, 'tcx>(
}

// Check that argument is Sized.
if !params_can_be_unsized {
let tail = fcx.tcx.struct_tail_with_normalize(param_ty, |ty| ty, || {});
if !params_can_be_unsized || matches!(tail.kind(), ty::Foreign(..)) {
fcx.require_type_is_sized(
param_ty,
param.pat.span,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_monomorphize/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ monomorphize_type_length_limit = reached the type-length limit while instantiati
monomorphize_unknown_cgu_collection_mode =
unknown codegen-item collection mode '{$mode}', falling back to 'lazy' mode

monomorphize_unsized_extern_fn_param = unsized arguments must not be `extern` types
monomorphize_unused_generic_params = item has unused generic parameters

monomorphize_written_to_path = the full type name has been written to '{$path}'
6 changes: 6 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,12 @@ fn visit_fn_use<'tcx>(
_ => bug!("failed to resolve instance for {ty}"),
}
};
for &param_ty in ty.fn_sig(tcx).inputs().skip_binder() {
let tail = tcx.struct_tail_with_normalize(param_ty, |ty| ty, || {});
if matches!(tail.kind(), ty::Foreign(..)) {
tcx.dcx().emit_fatal(errors::UnsizedExternParam { span: source });
}
}
visit_instance_use(tcx, instance, is_direct_call, source, output);
}
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_monomorphize/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ pub struct StartNotFound;
pub struct UnknownCguCollectionMode<'a> {
pub mode: &'a str,
}

#[derive(Diagnostic)]
#[diag(monomorphize_unsized_extern_fn_param)]
pub struct UnsizedExternParam {
#[primary_span]
pub span: Span,
}
17 changes: 16 additions & 1 deletion tests/ui/recursion/recursion.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
error: reached the recursion limit finding the struct tail for `Nil`
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]`

error: reached the recursion limit finding the struct tail for `Nil`
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
--> $DIR/recursion.rs:18:11
|
LL | _ => {test (n-1, i+1, Cons {head:2*i+1, tail:first}, Cons{head:i*i, tail:second})}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: reached the recursion limit while instantiating `test::<Cons<Cons<Cons<Cons<Cons<...>>>>>>`
--> $DIR/recursion.rs:18:11
|
Expand All @@ -11,5 +26,5 @@ LL | fn test<T:Dot> (n:isize, i:isize, first:T, second:T) ->isize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/recursion/recursion/recursion.long-type.txt'

error: aborting due to 1 previous error
error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://github.com/rust-lang/rust/issues/123887
// Do not ICE on unsized extern parameter
//@ compile-flags: -Clink-dead-code --emit=link
#![feature(extern_types, unsized_fn_params)]

extern "C" {
type ExternType;
}

struct Wrapper<T: ?Sized>(T);
fn f(_: impl ?Sized) {}
fn g(x: Box<Wrapper<ExternType>>) {
f(*x); //~ ERROR unsized arguments must not be `extern` types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unsized arguments must not be `extern` types
--> $DIR/extern-parameter-issue-123887-generic-wrapped.rs:13:5
|
LL | f(*x);
| ^^^^^

error: aborting due to 1 previous error

15 changes: 15 additions & 0 deletions tests/ui/unsized-locals/extern-parameter-issue-123887-generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://github.com/rust-lang/rust/issues/123887
// Do not ICE on unsized extern parameter
//@ compile-flags: -Clink-dead-code --emit=link
#![feature(extern_types, unsized_fn_params)]

extern "C" {
type ExternType;
}

fn f(_: impl ?Sized) {}
fn g(x: Box<ExternType>) {
f(*x); //~ ERROR unsized arguments must not be `extern` types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unsized arguments must not be `extern` types
--> $DIR/extern-parameter-issue-123887-generic.rs:12:5
|
LL | f(*x);
| ^^^^^

error: aborting due to 1 previous error

13 changes: 13 additions & 0 deletions tests/ui/unsized-locals/extern-parameter-issue-123887-wrapped.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://github.com/rust-lang/rust/issues/123887
// Do not ICE on unsized extern parameter
//@ compile-flags: -Clink-dead-code --emit=link
#![feature(extern_types, unsized_fn_params)]

extern "C" {
type ExternType;
}

struct Wrapper<T: ?Sized>(T);
fn f(_: Wrapper<ExternType>) {} //~ ERROR the size for values of type `ExternType` cannot be known at compilation time

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0277]: the size for values of type `ExternType` cannot be known at compilation time
--> $DIR/extern-parameter-issue-123887-wrapped.rs:11:6
|
LL | fn f(_: Wrapper<ExternType>) {}
| ^ doesn't have a size known at compile-time
|
= help: within `Wrapper<ExternType>`, the trait `Sized` is not implemented for `ExternType`, which is required by `Wrapper<ExternType>: Sized`
note: required because it appears within the type `Wrapper<ExternType>`
--> $DIR/extern-parameter-issue-123887-wrapped.rs:10:8
|
LL | struct Wrapper<T: ?Sized>(T);
| ^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn f(_: &Wrapper<ExternType>) {}
| +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
12 changes: 12 additions & 0 deletions tests/ui/unsized-locals/extern-parameter-issue-123887.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://github.com/rust-lang/rust/issues/123887
// Do not ICE on unsized extern parameter
//@ compile-flags: -Clink-dead-code --emit=link
#![feature(extern_types, unsized_fn_params)]

extern "C" {
type ExternType;
}

fn f(_: ExternType) {} //~ ERROR the size for values of type `ExternType` cannot be known at compilation time
eggyal marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what happens when we monomorphize some fn f<T: ?Sized>(_: T) by calling it with a value of ExternType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good point! I'm not hugely familiar with the monomorphization code, but have pushed a solution d98f42b. I'm not hugely comfortable with it though:

  1. it can trigger new recursion depth failures (perhaps this can't be avoided however?);
  2. I guess it could end up being evaluated for the same mono item multiple times, which would be unnecessary—but I don't quite understand the monomorphization process well enough to see how this can best be avoided?
  3. does it need a perf run?


fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/unsized-locals/extern-parameter-issue-123887.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0277]: the size for values of type `ExternType` cannot be known at compilation time
--> $DIR/extern-parameter-issue-123887.rs:10:6
|
LL | fn f(_: ExternType) {}
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `ExternType`
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn f(_: &ExternType) {}
| +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading