Skip to content

Commit

Permalink
fix suggestion for unsized function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Apr 19, 2021
1 parent 41f0e13 commit a8193ca
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 37 deletions.
13 changes: 6 additions & 7 deletions compiler/rustc_typeck/src/check/gather_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
use rustc_middle::ty::Ty;
use rustc_span::{sym, Span};
use rustc_trait_selection::traits;
use std::mem;

pub(super) struct GatherLocalsVisitor<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
parent_id: hir::HirId,
// parameters are special cases of patterns, but we want to handle them as
// *distinct* cases. so track when we are hitting a pattern *within* an fn
// parameter.
outermost_fn_param_pat: bool,
outermost_fn_param_pat: Option<Span>,
}

impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
pub(super) fn new(fcx: &'a FnCtxt<'a, 'tcx>, parent_id: hir::HirId) -> Self {
Self { fcx, parent_id, outermost_fn_param_pat: false }
Self { fcx, parent_id, outermost_fn_param_pat: None }
}

fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<LocalTy<'tcx>>) -> Ty<'tcx> {
Expand Down Expand Up @@ -92,7 +91,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
}

fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, true);
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.replace(param.ty_span);
intravisit::walk_param(self, param);
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
}
Expand All @@ -102,12 +101,12 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
if let PatKind::Binding(_, _, ident, _) = p.kind {
let var_ty = self.assign(p.span, p.hir_id, None);

if self.outermost_fn_param_pat {
if let Some(ty_span) = self.outermost_fn_param_pat {
if !self.fcx.tcx.features().unsized_fn_params {
self.fcx.require_type_is_sized(
var_ty,
p.span,
traits::SizedArgumentType(Some(p.span)),
traits::SizedArgumentType(Some(ty_span)),
);
}
} else {
Expand All @@ -123,7 +122,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
var_ty
);
}
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, false);
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.take();
intravisit::walk_pat(self, p);
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
}
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 @@ -9,8 +9,8 @@ LL | fn f(p: Path) { }
= 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 f(&p: Path) { }
| ^
LL | fn f(p: &Path) { }
| ^

error[E0277]: the trait bound `i32: Foo` is not satisfied
--> $DIR/E0277.rs:15:15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ LL | fn foo(x: dyn Foo) {
= 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 foo(&x: dyn Foo) {
| ^
LL | fn foo(x: &dyn Foo) {
| ^

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
Expand Down
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 @@ -8,8 +8,8 @@ LL | fn f(f: dyn FnOnce()) {}
= 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 f(&f: dyn FnOnce()) {}
| ^
LL | fn f(f: &dyn FnOnce()) {}
| ^

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-5883.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ struct Struct {
r: dyn A + 'static
}

fn new_struct(r: dyn A + 'static)
-> Struct { //~^ ERROR the size for values of type
//~^ ERROR the size for values of type
fn new_struct(
r: dyn A + 'static //~ ERROR the size for values of type
) -> Struct { //~ ERROR the size for values of type
Struct { r: r }
}

Expand Down
17 changes: 8 additions & 9 deletions src/test/ui/issues/issue-5883.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
--> $DIR/issue-5883.rs:7:15
--> $DIR/issue-5883.rs:8:5
|
LL | fn new_struct(r: dyn A + 'static)
| ^ doesn't have a size known at compile-time
LL | r: dyn A + 'static
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn A + 'static)`
= 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 new_struct(&r: dyn A + 'static)
| ^
LL | r: &dyn A + 'static
| ^

error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
--> $DIR/issue-5883.rs:8:8
--> $DIR/issue-5883.rs:9:6
|
LL | -> Struct {
| ^^^^^^ doesn't have a size known at compile-time
LL |
LL | ) -> Struct {
| ^^^^^^ doesn't have a size known at compile-time
LL | Struct { r: r }
| --------------- this returned value is of type `Struct`
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/resolve/issue-5035-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ LL | fn foo(_x: K) {}
= 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 foo(&_x: K) {}
| ^
LL | fn foo(_x: &K) {}
| ^

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/path-by-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ LL | fn f(p: Path) { }
= 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 f(&p: Path) { }
| ^
LL | fn f(p: &Path) { }
| ^

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/traits/bound/not-on-bare-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ LL | fn foo(_x: Foo + Send) {
= 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 foo(&_x: Foo + Send) {
| ^
LL | fn foo(_x: &Foo + Send) {
| ^

error: aborting due to previous error; 1 warning emitted

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/unsized/unsized-fn-arg.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix
#![crate_type="lib"]
#![allow(unused)]

fn f<T: ?Sized>(t: &T) {}
//~^ ERROR the size for values of type `T` cannot be known at compilation time
6 changes: 6 additions & 0 deletions src/test/ui/unsized/unsized-fn-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix
#![crate_type="lib"]
#![allow(unused)]

fn f<T: ?Sized>(t: T) {}
//~^ ERROR the size for values of type `T` cannot be known at compilation time
17 changes: 17 additions & 0 deletions src/test/ui/unsized/unsized-fn-arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/unsized-fn-arg.rs:5:17
|
LL | fn f<T: ?Sized>(t: T) {}
| - ^ doesn't have a size known at compile-time
| |
| this type parameter needs to be `std::marker::Sized`
|
= 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 f<T: ?Sized>(t: &T) {}
| ^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ LL | fn g1<X: ?Sized>(x: X) {}
= 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 g1<X: ?Sized>(&x: X) {}
| ^
LL | fn g1<X: ?Sized>(x: &X) {}
| ^

error[E0277]: the size for values of type `X` cannot be known at compilation time
--> $DIR/unsized6.rs:40:22
Expand All @@ -149,8 +149,8 @@ LL | fn g2<X: ?Sized + T>(x: X) {}
= 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 g2<X: ?Sized + T>(&x: X) {}
| ^
LL | fn g2<X: ?Sized + T>(x: &X) {}
| ^

error: aborting due to 13 previous errors

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/tools/clippy/tests/ui/crashes/ice-6251.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| x }]> {
= 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 bug<T>() -> impl Iterator<Item = [(); { |&x: [u8]| x }]> {
| ^
LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: &[u8]| x }]> {
| ^

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/ice-6251.rs:4:54
Expand Down

0 comments on commit a8193ca

Please sign in to comment.