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

Separate unsized fn params from unsized locals #74971

Closed
wants to merge 8 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 library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
#![feature(unicode_internals)]
#![feature(unsafe_block_in_unsafe_fn)]
#![feature(unsize)]
#![feature(unsized_locals)]
#![cfg_attr(not(bootstrap), feature(unsized_fn_params))]
#![cfg_attr(bootstrap, feature(unsized_locals))]
#![feature(allocator_internals)]
#![feature(slice_partition_dedup)]
#![feature(maybe_uninit_extra, maybe_uninit_slice)]
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
#![feature(stmt_expr_attributes)]
#![feature(transparent_unions)]
#![feature(unboxed_closures)]
#![feature(unsized_locals)]
#![cfg_attr(not(bootstrap), feature(unsized_fn_params))]
#![cfg_attr(bootstrap, feature(unsized_locals))]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]
#![feature(variant_count)]
Expand Down
7 changes: 4 additions & 3 deletions src/doc/unstable-book/src/language-features/unsized-locals.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ This implements [RFC1909]. When turned on, you can have unsized arguments and lo
[RFC1909]: https://github.com/rust-lang/rfcs/blob/master/text/1909-unsized-rvalues.md

```rust
#![feature(unsized_locals)]
#![allow(incomplete_features)]
#![feature(unsized_locals, unsized_fn_params)]

use std::any::Any;

Expand Down Expand Up @@ -85,7 +86,7 @@ fn main() {
With this feature, you can have by-value `self` arguments without `Self: Sized` bounds.

```rust
#![feature(unsized_locals)]
#![feature(unsized_fn_params)]

trait Foo {
fn foo(self) {}
Expand All @@ -102,7 +103,7 @@ fn main() {
And `Foo` will also be object-safe.

```rust
#![feature(unsized_locals)]
#![feature(unsized_fn_params)]

trait Foo {
fn foo(self) {}
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_feature/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ declare_features! (
/// Alloc calling `transmute` in const fn
(active, const_fn_transmute, "1.46.0", Some(53605), None),

/// Allows unsized fn parameters.
(active, unsized_fn_params, "1.47.0", Some(48055), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand All @@ -597,4 +600,5 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
sym::const_trait_bound_opt_out,
sym::lazy_normalization_consts,
sym::specialization,
sym::unsized_locals,
];
14 changes: 8 additions & 6 deletions src/librustc_mir/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

self.check_rvalue(body, rv, location);
if !self.tcx().features().unsized_locals {
if !(self.tcx().features().unsized_locals
|| self.tcx().features().unsized_fn_params)
{
let trait_ref = ty::TraitRef {
def_id: tcx.require_lang_item(SizedTraitLangItem, Some(self.last_span)),
substs: tcx.mk_substs_trait(place_ty, &[]),
Expand Down Expand Up @@ -1732,9 +1734,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
}

// When `#![feature(unsized_locals)]` is not enabled,
// When `unsized_fn_params` or `unsized_locals` is not enabled,
// this check is done at `check_local`.
if self.tcx().features().unsized_locals {
if self.tcx().features().unsized_locals || self.tcx().features().unsized_fn_params {
let span = term.source_info.span;
self.ensure_place_sized(dest_ty, span);
}
Expand Down Expand Up @@ -1895,9 +1897,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
LocalKind::Var | LocalKind::Temp => {}
}

// When `#![feature(unsized_locals)]` is enabled, only function calls
// When `unsized_fn_params` or `unsized_locals` is enabled, only function calls
// and nullary ops are checked in `check_call_dest`.
if !self.tcx().features().unsized_locals {
if !(self.tcx().features().unsized_locals || self.tcx().features().unsized_fn_params) {
let span = local_decl.source_info.span;
let ty = local_decl.ty;
self.ensure_place_sized(ty, span);
Expand Down Expand Up @@ -2043,7 +2045,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

Rvalue::NullaryOp(_, ty) => {
// Even with unsized locals cannot box an unsized value.
if self.tcx().features().unsized_locals {
if self.tcx().features().unsized_locals || self.tcx().features().unsized_fn_params {
let span = body.source_info(location).span;
self.ensure_place_sized(ty, span);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let tcx = this.hir.tcx();

if tcx.features().unsized_locals {
if tcx.features().unsized_fn_params {
let ty = expr.ty;
let span = expr.span;
let param_env = this.hir.param_env;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ symbols! {
unsafe_cell,
unsafe_no_drop_flag,
unsize,
unsized_fn_params,
unsized_locals,
unsized_tuple_coercion,
unstable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1833,9 +1833,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err.note("all function arguments must have a statically known size");
}
if tcx.sess.opts.unstable_features.is_nightly_build()
&& !self.tcx.features().unsized_locals
&& !self.tcx.features().unsized_fn_params
{
err.help("unsized locals are gated as an unstable feature");
err.help("unsized fn params are gated as an unstable feature");
}
}
ObligationCauseCode::SizedReturnType => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

if let ty::FnDef(..) = ty.kind {
let fn_sig = ty.fn_sig(tcx);
if !tcx.features().unsized_locals {
if !tcx.features().unsized_fn_params {
// We want to remove some Sized bounds from std functions,
// but don't want to expose the removal to stable Rust.
// i.e., we don't want to allow
Expand Down
28 changes: 23 additions & 5 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,8 @@ fn typeck_with_fallback<'tcx>(
};

// Gather locals in statics (because of block expressions).
GatherLocalsVisitor { fcx: &fcx, parent_id: id }.visit_body(body);
GatherLocalsVisitor { fcx: &fcx, parent_id: id, within_fn_param: false }
.visit_body(body);

fcx.check_expr_coercable_to_type(&body.value, revealed_ty, None);

Expand Down Expand Up @@ -1154,6 +1155,10 @@ fn check_abi(tcx: TyCtxt<'_>, span: Span, abi: Abi) {
struct GatherLocalsVisitor<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
parent_id: hir::HirId,
// params are special cases of pats, but we want to handle them as
// *distinct* cases. so track when we are hitting a pat *within* an fn
// param.
within_fn_param: bool,
}

impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
Expand Down Expand Up @@ -1228,13 +1233,25 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
intravisit::walk_local(self, local);
}

fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
self.within_fn_param = true;
intravisit::walk_param(self, param);
self.within_fn_param = false;
}

// Add pattern bindings.
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
if let PatKind::Binding(_, _, ident, _) = p.kind {
let var_ty = self.assign(p.span, p.hir_id, None);

if !self.fcx.tcx.features().unsized_locals {
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
if self.within_fn_param {
if !self.fcx.tcx.features().unsized_fn_params {
self.fcx.require_type_is_sized(var_ty, p.span, traits::SizedArgumentType(None));
}
} else {
if !self.fcx.tcx.features().unsized_locals {
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
}
}
Copy link
Member

Choose a reason for hiding this comment

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

What about fn a(box box b: Box<Box<[u8]>>) {}? I think that should keep using unsized_locals and not unsized_fn_params.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I assume that pattern will generate MIR with unsized locals.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm yeah, confirmed that it won't work well in this PR. How can we catch such a case?

Copy link
Member

Choose a reason for hiding this comment

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

I have no familiarity with HIR, but it must be possible to somehow check if the function argument is a "trivial" pattern, right?

Copy link
Member

Choose a reason for hiding this comment

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

The params should all have a pat that's immediately Binding.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have no familiarity with HIR, but it must be possible to somehow check if the function argument is a "trivial" pattern, right?

I think p.simple_ident could be used here as well as https://github.com/rust-lang/rust/pull/74971/files#diff-1d1b0d29a2e8da97c6bfb6e364d920c7R1378-R1381?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe?^^ I'm really the wrong person to ask such questions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, anyway thanks for pointing out :) Let's move this topic to Zulip so that the we can get some eyes from other members...

Copy link
Member

Choose a reason for hiding this comment

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

(summarizing my discussion with @spastorino on Zulip)
I think a bigger issue than special-casing the shape of the pattern, is that within_fn_param applies recursively to every nested pattern in a fn parameter, instead of to the parameter pattern itself.

So my suggestion is to replace it with a "shallow" version (bikeshed name: outermost_fn_param_pat) that visit_pat temporarily sets back to false around its walk_pat call, so that the #![feature(unsized_fn_params)] path is only taken for fn parameters themselves, not any other patterns nested in them.


debug!(
Expand Down Expand Up @@ -1334,7 +1351,8 @@ fn check_fn<'a, 'tcx>(

let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()).expect_local();
let outer_hir_id = hir.as_local_hir_id(outer_def_id);
GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body);
GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id, within_fn_param: false }
.visit_body(body);

// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
// (as it's created inside the body itself, not passed in from outside).
Expand All @@ -1360,7 +1378,7 @@ fn check_fn<'a, 'tcx>(
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
// for simple cases like `fn foo(x: Trait)`,
// where we would error once on the parameter as a whole, and once on the binding `x`.
if param.pat.simple_ident().is_none() && !tcx.features().unsized_locals {
if param.pat.simple_ident().is_none() && !tcx.features().unsized_fn_params {
fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(unsized_locals)]
#![allow(incomplete_features)]
#![feature(unsized_locals, unsized_fn_params)]

use std::fmt;

Expand Down Expand Up @@ -45,11 +46,7 @@ fn main() {

{
let x: fmt::Display = *gen_foo();
let x = if true {
x
} else {
*gen_foo()
};
let x = if true { x } else { *gen_foo() };
foo(x);
}
}
7 changes: 2 additions & 5 deletions src/test/ui/async-await/issue-72590-type-error-sized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ LL | async fn frob(self) {}
|
= help: within `Foo`, the trait `std::marker::Sized` is not implemented for `str`
= note: required because it appears within the type `Foo`
= help: unsized locals are gated as an unstable feature
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | async fn frob(&self) {}
| ^
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error: aborting due to 3 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0161.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
//[zflagsul]compile-flags: -Z borrowck=migrate
//[editionul]edition:2018

#![allow(incomplete_features)]
#![cfg_attr(nll, feature(nll))]
#![cfg_attr(nllul, feature(nll))]
#![cfg_attr(migrateul, feature(unsized_locals))]
#![cfg_attr(zflagsul, feature(unsized_locals))]
#![cfg_attr(nllul, feature(unsized_locals))]
#![cfg_attr(editionul, feature(unsized_locals))]

#![feature(box_syntax)]

fn foo(x: Box<[i32]>) {
Expand Down
7 changes: 2 additions & 5 deletions src/test/ui/error-codes/E0277.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ LL | fn f(p: Path) { }
|
= help: within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
= note: required because it appears within the type `std::path::Path`
= help: unsized locals are gated as an unstable feature
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn f(p: &Path) { }
| ^
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error[E0277]: the trait bound `i32: Foo` is not satisfied
--> $DIR/E0277.rs:17:15
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/feature-gates/feature-gate-unsized_fn_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[repr(align(256))]
#[allow(dead_code)]
struct A {
v: u8,
}

trait Foo {
fn foo(&self);
}

impl Foo for A {
fn foo(&self) {
assert_eq!(self as *const A as usize % 256, 0);
}
}

fn foo(x: dyn Foo) {
//~^ ERROR [E0277]
x.foo()
}

fn main() {
let x: Box<dyn Foo> = Box::new(A { v: 22 });
foo(*x);
//~^ ERROR [E0277]
}
23 changes: 23 additions & 0 deletions src/test/ui/feature-gates/feature-gate-unsized_fn_params.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
--> $DIR/feature-gate-unsized_fn_params.rs:17:8
|
LL | fn foo(x: dyn Foo) {
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn Foo + 'static)`
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
--> $DIR/feature-gate-unsized_fn_params.rs:24:5
|
LL | foo(*x);
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn Foo + 'static)`
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
7 changes: 2 additions & 5 deletions src/test/ui/feature-gates/feature-gate-unsized_locals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ LL | fn f(f: dyn FnOnce()) {}
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::ops::FnOnce() + 'static)`
= help: unsized locals are gated as an unstable feature
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn f(f: &dyn FnOnce()) {}
| ^
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error: aborting due to previous error

Expand Down
1 change: 0 additions & 1 deletion src/test/ui/fn/dyn-fn-alignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// run-pass

#![feature(unsized_locals)]
#![allow(dead_code)]
#[repr(align(256))]
struct A {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-17651.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | (|| Box::new(*(&[0][..])))();
|
= help: the trait `std::marker::Sized` is not implemented for `[{integer}]`
= note: all function arguments must have a statically known size
= help: unsized locals are gated as an unstable feature
= help: unsized fn params are gated as an unstable feature

error: aborting due to 2 previous errors

Expand Down
7 changes: 2 additions & 5 deletions src/test/ui/issues/issue-27078.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
LL | fn foo(self) -> &'static i32 {
| ^^^^ doesn't have a size known at compile-time
|
= help: unsized locals are gated as an unstable feature
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature
help: consider further restricting `Self`
|
LL | fn foo(self) -> &'static i32 where Self: std::marker::Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn foo(&self) -> &'static i32 {
| ^

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-30355.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | &X(*Y)
|
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
= note: all function arguments must have a statically known size
= help: unsized locals are gated as an unstable feature
= help: unsized fn params are gated as an unstable feature

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-38954.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | fn _test(ref _p: str) {}
| ^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= help: unsized locals are gated as an unstable feature
= help: unsized fn params are gated as an unstable feature
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn _test(ref _p: &str) {}
Expand Down
Loading