Skip to content

Commit 279f1c9

Browse files
committed
Auto merge of #106004 - fee1-dead-contrib:const-closures, r=oli-obk
Const closures cc #106003
2 parents bfffe40 + 42a50ba commit 279f1c9

Some content is hidden

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

56 files changed

+249
-48
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ impl Expr {
13071307
pub struct Closure {
13081308
pub binder: ClosureBinder,
13091309
pub capture_clause: CaptureBy,
1310+
pub constness: Const,
13101311
pub asyncness: Async,
13111312
pub movability: Movability,
13121313
pub fn_decl: P<FnDecl>,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13621362
ExprKind::Closure(box Closure {
13631363
binder,
13641364
capture_clause: _,
1365+
constness,
13651366
asyncness,
13661367
movability: _,
13671368
fn_decl,
@@ -1370,6 +1371,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13701371
fn_arg_span: _,
13711372
}) => {
13721373
vis.visit_closure_binder(binder);
1374+
visit_constness(constness, vis);
13731375
vis.visit_asyncness(asyncness);
13741376
vis.visit_fn_decl(fn_decl);
13751377
vis.visit_expr(body);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
836836
binder,
837837
capture_clause: _,
838838
asyncness: _,
839+
constness: _,
839840
movability: _,
840841
fn_decl,
841842
body,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
209209
ExprKind::Closure(box Closure {
210210
binder,
211211
capture_clause,
212+
constness,
212213
asyncness,
213214
movability,
214215
fn_decl,
@@ -233,6 +234,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
233234
binder,
234235
*capture_clause,
235236
e.id,
237+
*constness,
236238
*movability,
237239
fn_decl,
238240
body,
@@ -651,6 +653,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
651653
fn_decl_span: self.lower_span(span),
652654
fn_arg_span: None,
653655
movability: Some(hir::Movability::Static),
656+
constness: hir::Constness::NotConst,
654657
});
655658

656659
hir::ExprKind::Closure(c)
@@ -890,6 +893,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
890893
binder: &ClosureBinder,
891894
capture_clause: CaptureBy,
892895
closure_id: NodeId,
896+
constness: Const,
893897
movability: Movability,
894898
decl: &FnDecl,
895899
body: &Expr,
@@ -927,6 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
927931
fn_decl_span: self.lower_span(fn_decl_span),
928932
fn_arg_span: Some(self.lower_span(fn_arg_span)),
929933
movability: generator_option,
934+
constness: self.lower_constness(constness),
930935
});
931936

932937
hir::ExprKind::Closure(c)
@@ -1041,6 +1046,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10411046
fn_decl_span: self.lower_span(fn_decl_span),
10421047
fn_arg_span: Some(self.lower_span(fn_arg_span)),
10431048
movability: None,
1049+
constness: hir::Constness::NotConst,
10441050
});
10451051
hir::ExprKind::Closure(c)
10461052
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12391239
}
12401240
}
12411241

1242-
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1242+
pub(super) fn lower_constness(&mut self, c: Const) -> hir::Constness {
12431243
match c {
12441244
Const::Yes(_) => hir::Constness::Const,
12451245
Const::No => hir::Constness::NotConst,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
385385
ast::ExprKind::TryBlock(_) => {
386386
gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
387387
}
388+
ast::ExprKind::Closure(box ast::Closure { constness: ast::Const::Yes(_), .. }) => {
389+
gate_feature_post!(
390+
&self,
391+
const_closures,
392+
e.span,
393+
"const closures are experimental"
394+
);
395+
}
388396
_ => {}
389397
}
390398
visit::walk_expr(self, e)

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ impl<'a> State<'a> {
399399
ast::ExprKind::Closure(box ast::Closure {
400400
binder,
401401
capture_clause,
402+
constness,
402403
asyncness,
403404
movability,
404405
fn_decl,
@@ -407,6 +408,7 @@ impl<'a> State<'a> {
407408
fn_arg_span: _,
408409
}) => {
409410
self.print_closure_binder(binder);
411+
self.print_constness(*constness);
410412
self.print_movability(*movability);
411413
self.print_asyncness(*asyncness);
412414
self.print_capture_clause(*capture_clause);

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
4141
};
4242
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
4343
}
44+
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
4445
_ => {
4546
if let Some(fn_kind) = node.fn_kind() {
4647
if fn_kind.constness() == hir::Constness::Const {

compiler/rustc_const_eval/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Rust MIR: a lowered representation of Rust.
2020
#![feature(trusted_step)]
2121
#![feature(try_blocks)]
2222
#![feature(yeet_expr)]
23+
#![feature(if_let_guard)]
2324
#![feature(is_some_and)]
2425
#![recursion_limit = "256"]
2526

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
242242
// impl trait is gone in MIR, so check the return type of a const fn by its signature
243243
// instead of the type of the return place.
244244
self.span = body.local_decls[RETURN_PLACE].source_info.span;
245-
let return_ty = tcx.fn_sig(def_id).output();
245+
let return_ty = self.ccx.fn_sig().output();
246246
self.check_local_or_return_ty(return_ty.skip_binder(), RETURN_PLACE);
247247
}
248248

@@ -730,6 +730,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
730730
substs,
731731
span: *fn_span,
732732
from_hir_call: *from_hir_call,
733+
feature: Some(sym::const_trait_impl),
733734
});
734735
return;
735736
}
@@ -782,6 +783,20 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
782783
);
783784
return;
784785
}
786+
Ok(Some(ImplSource::Closure(data))) => {
787+
if !tcx.is_const_fn_raw(data.closure_def_id) {
788+
self.check_op(ops::FnCallNonConst {
789+
caller,
790+
callee,
791+
substs,
792+
span: *fn_span,
793+
from_hir_call: *from_hir_call,
794+
feature: None,
795+
});
796+
797+
return;
798+
}
799+
}
785800
Ok(Some(ImplSource::UserDefined(data))) => {
786801
let callee_name = tcx.item_name(callee);
787802
if let Some(&did) = tcx
@@ -802,6 +817,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
802817
substs,
803818
span: *fn_span,
804819
from_hir_call: *from_hir_call,
820+
feature: None,
805821
});
806822
return;
807823
}
@@ -844,6 +860,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
844860
substs,
845861
span: *fn_span,
846862
from_hir_call: *from_hir_call,
863+
feature: None,
847864
});
848865
return;
849866
}
@@ -903,6 +920,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
903920
substs,
904921
span: *fn_span,
905922
from_hir_call: *from_hir_call,
923+
feature: None,
906924
});
907925
return;
908926
}

0 commit comments

Comments
 (0)