Skip to content

Commit 346aeef

Browse files
authored
Rollup merge of rust-lang#78152 - spastorino:separate-unsized-locals, r=oli-obk
Separate unsized locals Closes rust-lang#71694 Takes over again rust-lang#72029 and rust-lang#74971 cc @RalfJung @oli-obk @pnkfelix @eddyb as they've participated in previous reviews of this PR.
2 parents 07e968b + cc9ab1c commit 346aeef

File tree

62 files changed

+354
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+354
-147
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ declare_features! (
607607
/// Allow anonymous constants from an inline `const` block
608608
(active, inline_const, "1.49.0", Some(76001), None),
609609

610+
/// Allows unsized fn parameters.
611+
(active, unsized_fn_params, "1.49.0", Some(48055), None),
612+
610613
// -------------------------------------------------------------------------
611614
// feature-group-end: actual feature gates
612615
// -------------------------------------------------------------------------
@@ -629,6 +632,7 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
629632
sym::specialization,
630633
sym::inline_const,
631634
sym::repr128,
635+
sym::unsized_locals,
632636
];
633637

634638
/// Some features are not allowed to be used together at the same time, if

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
974974
checker
975975
}
976976

977+
fn unsized_feature_enabled(&self) -> bool {
978+
let features = self.tcx().features();
979+
features.unsized_locals || features.unsized_fn_params
980+
}
981+
977982
/// Equate the inferred type and the annotated type for user type annotations
978983
fn check_user_type_annotations(&mut self) {
979984
debug!(
@@ -1456,7 +1461,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14561461
}
14571462

14581463
self.check_rvalue(body, rv, location);
1459-
if !self.tcx().features().unsized_locals {
1464+
if !self.unsized_feature_enabled() {
14601465
let trait_ref = ty::TraitRef {
14611466
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
14621467
substs: tcx.mk_substs_trait(place_ty, &[]),
@@ -1717,9 +1722,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17171722
);
17181723
}
17191724

1720-
// When `#![feature(unsized_locals)]` is not enabled,
1725+
// When `unsized_fn_params` and `unsized_locals` are both not enabled,
17211726
// this check is done at `check_local`.
1722-
if self.tcx().features().unsized_locals {
1727+
if self.unsized_feature_enabled() {
17231728
let span = term.source_info.span;
17241729
self.ensure_place_sized(dest_ty, span);
17251730
}
@@ -1880,9 +1885,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18801885
LocalKind::Var | LocalKind::Temp => {}
18811886
}
18821887

1883-
// When `#![feature(unsized_locals)]` is enabled, only function calls
1888+
// When `unsized_fn_params` or `unsized_locals` is enabled, only function calls
18841889
// and nullary ops are checked in `check_call_dest`.
1885-
if !self.tcx().features().unsized_locals {
1890+
if !self.unsized_feature_enabled() {
18861891
let span = local_decl.source_info.span;
18871892
let ty = local_decl.ty;
18881893
self.ensure_place_sized(ty, span);
@@ -2024,7 +2029,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20242029

20252030
Rvalue::NullaryOp(_, ty) => {
20262031
// Even with unsized locals cannot box an unsized value.
2027-
if self.tcx().features().unsized_locals {
2032+
if self.unsized_feature_enabled() {
20282033
let span = body.source_info(location).span;
20292034
self.ensure_place_sized(ty, span);
20302035
}

compiler/rustc_mir_build/src/build/expr/as_operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
165165

166166
let tcx = this.hir.tcx();
167167

168-
if tcx.features().unsized_locals {
168+
if tcx.features().unsized_fn_params {
169169
let ty = expr.ty;
170170
let span = expr.span;
171171
let param_env = this.hir.param_env;

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ symbols! {
11631163
unsafe_cell,
11641164
unsafe_no_drop_flag,
11651165
unsize,
1166+
unsized_fn_params,
11661167
unsized_locals,
11671168
unsized_tuple_coercion,
11681169
unstable,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,9 +1845,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18451845
err.note("all function arguments must have a statically known size");
18461846
}
18471847
if tcx.sess.opts.unstable_features.is_nightly_build()
1848-
&& !self.tcx.features().unsized_locals
1848+
&& !self.tcx.features().unsized_fn_params
18491849
{
1850-
err.help("unsized locals are gated as an unstable feature");
1850+
err.help("unsized fn params are gated as an unstable feature");
18511851
}
18521852
}
18531853
ObligationCauseCode::SizedReturnType => {

compiler/rustc_typeck/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub(super) fn check_fn<'a, 'tcx>(
131131
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
132132
// for simple cases like `fn foo(x: Trait)`,
133133
// where we would error once on the parameter as a whole, and once on the binding `x`.
134-
if param.pat.simple_ident().is_none() && !tcx.features().unsized_locals {
134+
if param.pat.simple_ident().is_none() && !tcx.features().unsized_fn_params {
135135
fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span));
136136
}
137137

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
476476

477477
if let ty::FnDef(..) = ty.kind() {
478478
let fn_sig = ty.fn_sig(tcx);
479-
if !tcx.features().unsized_locals {
479+
if !tcx.features().unsized_fn_params {
480480
// We want to remove some Sized bounds from std functions,
481481
// but don't want to expose the removal to stable Rust.
482482
// i.e., we don't want to allow

compiler/rustc_typeck/src/check/gather_locals.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
66
use rustc_middle::ty::Ty;
77
use rustc_span::Span;
88
use rustc_trait_selection::traits;
9+
use std::mem;
910

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

1520
impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
1621
pub(super) fn new(fcx: &'a FnCtxt<'a, 'tcx>, parent_id: hir::HirId) -> Self {
17-
Self { fcx, parent_id }
22+
Self { fcx, parent_id, outermost_fn_param_pat: false }
1823
}
1924

2025
fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<LocalTy<'tcx>>) -> Ty<'tcx> {
@@ -88,13 +93,29 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
8893
intravisit::walk_local(self, local);
8994
}
9095

96+
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
97+
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, true);
98+
intravisit::walk_param(self, param);
99+
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
100+
}
101+
91102
// Add pattern bindings.
92103
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
93104
if let PatKind::Binding(_, _, ident, _) = p.kind {
94105
let var_ty = self.assign(p.span, p.hir_id, None);
95106

96-
if !self.fcx.tcx.features().unsized_locals {
97-
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
107+
if self.outermost_fn_param_pat {
108+
if !self.fcx.tcx.features().unsized_fn_params {
109+
self.fcx.require_type_is_sized(
110+
var_ty,
111+
p.span,
112+
traits::SizedArgumentType(Some(p.span)),
113+
);
114+
}
115+
} else {
116+
if !self.fcx.tcx.features().unsized_locals {
117+
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
118+
}
98119
}
99120

100121
debug!(
@@ -104,7 +125,9 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
104125
var_ty
105126
);
106127
}
128+
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, false);
107129
intravisit::walk_pat(self, p);
130+
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
108131
}
109132

110133
// Don't descend into the bodies of nested closures.

library/alloc/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@
130130
#![feature(unicode_internals)]
131131
#![feature(unsafe_block_in_unsafe_fn)]
132132
#![feature(unsize)]
133-
#![feature(unsized_locals)]
133+
#![cfg_attr(not(bootstrap), feature(unsized_fn_params))]
134+
#![cfg_attr(bootstrap, feature(unsized_locals))]
134135
#![feature(allocator_internals)]
135136
#![feature(slice_partition_dedup)]
136137
#![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_uninit_array)]

library/core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@
132132
#![feature(transparent_unions)]
133133
#![feature(try_blocks)]
134134
#![feature(unboxed_closures)]
135-
#![feature(unsized_locals)]
135+
#![cfg_attr(not(bootstrap), feature(unsized_fn_params))]
136+
#![cfg_attr(bootstrap, feature(unsized_locals))]
136137
#![cfg_attr(bootstrap, feature(untagged_unions))]
137138
#![feature(unwind_attributes)]
138139
#![feature(variant_count)]

0 commit comments

Comments
 (0)