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 param from unsized locals #72029

Closed
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
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
3 changes: 2 additions & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
#![feature(unboxed_closures)]
#![feature(unicode_internals)]
#![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
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
#![feature(track_caller)]
#![feature(transparent_unions)]
#![feature(unboxed_closures)]
#![cfg_attr(not(bootstrap), feature(unsized_fn_params))]
#![feature(unsized_locals)]
Copy link
Member

Choose a reason for hiding this comment

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

unsized_locals should not be needed any more, so can you gate this by cfg_attr(bootstrap, ...)?

Copy link
Member Author

Choose a reason for hiding this comment

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

If I do this change in libcore I get ...

Building stage1 std artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
   Compiling cc v1.0.52
   Compiling core v0.0.0 (/home/santiago/src/oss/rust2/src/libcore)
   Compiling libc v0.2.69
   Compiling autocfg v0.1.7
   Compiling std v0.0.0 (/home/santiago/src/oss/rust2/src/libstd)
   Compiling hashbrown v0.6.2
   Compiling compiler_builtins v0.1.28
   Compiling backtrace-sys v0.1.37
   Compiling unwind v0.0.0 (/home/santiago/src/oss/rust2/src/libunwind)
error[E0161]: cannot move a value of type T: the size of T cannot be statically determined
   --> src/libcore/mem/mod.rs:162:33
    |
162 |     unsafe { intrinsics::forget(t) }
    |                                 ^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0161`.
error: could not compile `core`.

To learn more, run the command again with --verbose.
command did not execute successfully: "/home/santiago/src/oss/rust2/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "-Zbinary-dep-depinfo" "-j" "8" "--release" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/home/santiago/src/oss/rust2/src/libtest/Cargo.toml" "--message-format" "json-render-diagnostics"
expected success, got: exit code: 101
failed to run: /home/santiago/src/oss/rust2/build/bootstrap/debug/bootstrap build --stage 1 src/libstd
Build completed unsuccessfully in 0:02:00

Copy link
Member

Choose a reason for hiding this comment

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

Something is wrong then. The unsound feature must not be used in libcore/liballoc.

Copy link
Member Author

@spastorino spastorino May 26, 2020

Choose a reason for hiding this comment

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

If I'm not wrong this is the last missing bit to fix but I don't understand it exactly.
Wasn't libcore/liballoc already using that?. Isn't fixing some of this stuff for a different PR? or am I not understanding what you're suggesting?.

Copy link
Member

@RalfJung RalfJung May 26, 2020

Choose a reason for hiding this comment

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

Wasn't libcore/liballoc already using that?

Using what?

libstd/liballoc/libcore (and all of rustc really except for the test suite) should only be using the sound version of the feature. After all, we don't want unsound code in our standard library or the compiler. So clearly unsized_locals should not be enabled. Does this make sense so far?

The claim, back when passing unsized parameters to functions was carved out as a "soundly implementable subset", was that this would suffice for what libstd is doing. So assuming that claim is correct, no further changes should be needed to disable unsized_locals.

However, the bootstrap compiler does not understand unsized_fn_params, so we still have to enable the unsound unsized_locals but only when building the standard library with the bootstrap compiler.

Copy link
Member

Choose a reason for hiding this comment

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

The error here looks like this is just an unsized parameter, not an arbitrary unsized local. So probably, your PR has a bug somewhere where it is still checking the wrong feature gate at some place.

#![feature(untagged_unions)]
#![feature(unwind_attributes)]
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 @@ -571,6 +571,9 @@ declare_features! (
/// Allows the use of `#[ffi_const]` on foreign functions.
(active, ffi_const, "1.45.0", Some(58328), None),

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

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand All @@ -587,4 +590,5 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
sym::raw_dylib,
sym::const_trait_impl,
sym::const_trait_bound_opt_out,
sym::unsized_locals,
];
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

// When `#![feature(unsized_locals)]` is not enabled,
// this check is done at `check_local`.
if self.tcx().features().unsized_locals {
if self.tcx().features().unsized_fn_params {
let span = term.source_info.span;
self.ensure_place_sized(dest_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 @@ -807,6 +807,7 @@ symbols! {
unreachable_code,
unrestricted_attribute_tokens,
unsafe_no_drop_flag,
unsized_fn_params,
unsized_locals,
unsized_tuple_coercion,
unstable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1749,8 +1749,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}
ObligationCauseCode::SizedArgumentType => {
err.note("all function arguments must have a statically known size");
if !self.tcx.features().unsized_locals {
err.help("unsized locals are gated as an unstable feature");
if !self.tcx.features().unsized_fn_params {
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 @@ -486,7 +486,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 @@ -1053,7 +1053,8 @@ fn typeck_tables_of_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);

Expand Down Expand Up @@ -1156,6 +1157,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 @@ -1226,13 +1231,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);
}
} else {
if !self.fcx.tcx.features().unsized_locals {
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
}
}

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

let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id());
let outer_hir_id = hir.as_local_hir_id(outer_def_id.expect_local());
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 Down Expand Up @@ -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);
}

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);
}
}
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
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0277.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LL | fn f(p: Path) { }
= help: within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because it appears within the type `std::path::Path`
= note: all local variables must have a statically known size
= 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

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]
}
25 changes: 25 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,25 @@
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: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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`.
4 changes: 2 additions & 2 deletions src/test/ui/feature-gates/feature-gate-unsized_locals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | fn f(f: dyn FnOnce()) {}
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::ops::FnOnce() + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: all local variables must have a statically known size
= 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

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)]
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
#![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 @@ -17,7 +17,7 @@ LL | (|| Box::new(*(&[0][..])))();
= help: the trait `std::marker::Sized` is not implemented for `[{integer}]`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-27078.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | fn foo(self) -> &'static i32 {
|
= help: the trait `std::marker::Sized` is not implemented for `Self`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: all local variables must have a statically known size
= 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 {
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 @@ -7,7 +7,7 @@ LL | &X(*Y)
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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 @@ -7,7 +7,7 @@ LL | fn _test(ref _p: str) {}
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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-41229-ref-str.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub fn example(ref s: str) {}
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-42312.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn baz(_: Self::Target) where Self: Deref {}
= help: the trait `std::marker::Sized` is not implemented for `<Self as std::ops::Deref>::Target`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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
help: consider further restricting the associated type
|
LL | fn baz(_: Self::Target) where Self: Deref, <Self as std::ops::Deref>::Target: std::marker::Sized {}
Expand All @@ -22,7 +22,7 @@ LL | pub fn f(_: dyn ToString) {}
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::string::ToString + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= 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
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-5883.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | fn new_struct(r: dyn A + 'static)
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn A + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: all local variables must have a statically known size
= 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

error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
--> $DIR/issue-5883.rs:8:8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![feature(arbitrary_self_types, coerce_unsized, dispatch_from_dyn, unsize, unsized_locals)]
#![feature(arbitrary_self_types, coerce_unsized, dispatch_from_dyn, unsize)]
#![feature(unsized_locals, unsized_fn_params)]
//~^ WARN the feature `unsized_locals` is incomplete

// This tests a few edge-cases around `arbitrary_self_types`. Most specifically,
// it checks that the `ObjectCandidate` you get from method matching can't
Expand Down
Loading