Skip to content

Don't ICE on unsized extern "rust-call" call #111885

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

Merged
merged 2 commits into from
Jun 13, 2023
Merged
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
8 changes: 7 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,14 +1449,20 @@ fn check_fn_or_method<'tcx>(
let span = tcx.def_span(def_id);
let has_implicit_self = hir_decl.implicit_self != hir::ImplicitSelfKind::None;
let mut inputs = sig.inputs().iter().skip(if has_implicit_self { 1 } else { 0 });
// Check that the argument is a tuple
// Check that the argument is a tuple and is sized
if let Some(ty) = inputs.next() {
wfcx.register_bound(
ObligationCause::new(span, wfcx.body_def_id, ObligationCauseCode::RustCall),
wfcx.param_env,
*ty,
tcx.require_lang_item(hir::LangItem::Tuple, Some(span)),
);
wfcx.register_bound(
ObligationCause::new(span, wfcx.body_def_id, ObligationCauseCode::RustCall),
wfcx.param_env,
*ty,
tcx.require_lang_item(hir::LangItem::Sized, Some(span)),
);
} else {
tcx.sess.span_err(
hir_decl.inputs.last().map_or(span, |input| input.span),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.require_lang_item(hir::LangItem::Tuple, Some(sp)),
traits::ObligationCause::new(sp, self.body_id, traits::RustCall),
);
self.require_type_is_sized(ty, sp, traits::RustCall);
} else {
self.tcx.sess.span_err(
sp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2663,9 +2663,15 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
| ObligationCauseCode::LetElse
| ObligationCauseCode::BinOp { .. }
| ObligationCauseCode::AscribeUserTypeProvePredicate(..)
| ObligationCauseCode::RustCall
| ObligationCauseCode::DropImpl
| ObligationCauseCode::ConstParam(_) => {}
ObligationCauseCode::RustCall => {
if let Some(pred) = predicate.to_opt_poly_trait_pred()
&& Some(pred.def_id()) == self.tcx.lang_items().sized_trait()
{
err.note("argument required to be sized due to `extern \"rust-call\"` ABI");
}
}
ObligationCauseCode::SliceOrArrayElem => {
err.note("slice and array elements must have `Sized` type");
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/unsized-locals/rust-call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(unsized_tuple_coercion)]
#![feature(unboxed_closures)]
#![feature(unsized_fn_params)]

fn bad() -> extern "rust-call" fn(([u8],)) { todo!() }
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if one should still allow declaring the function. A case could be made for functions with generics as you could call them with a sized type parameter, but if we know that the type is unsized maybe we could error? Or maybe it should be a lint instead? IDK.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I don't know if we could make this a well-formedness check. Maybe a lint, yeah.


fn main() {
let f = bad();
let slice: Box<([u8],)> = Box::new(([1; 8],));
f(*slice);
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
}
13 changes: 13 additions & 0 deletions tests/ui/unsized-locals/rust-call.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/rust-call.rs:10:7
|
LL | f(*slice);
| ^^^^^^ doesn't have a size known at compile-time
|
= help: within `([u8],)`, the trait `Sized` is not implemented for `[u8]`
= note: required because it appears within the type `([u8],)`
= note: argument required to be sized due to `extern "rust-call"` ABI

error: aborting due to previous error

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