Skip to content

Commit d2e1ddc

Browse files
committed
const_items_unit_type_default: initial implementation
1 parent 16b9a8a commit d2e1ddc

File tree

16 files changed

+149
-84
lines changed

16 files changed

+149
-84
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3769,7 +3769,7 @@ pub struct ConstItem {
37693769
pub defaultness: Defaultness,
37703770
pub ident: Ident,
37713771
pub generics: Generics,
3772-
pub ty: Box<Ty>,
3772+
pub ty: FnRetTy,
37733773
pub rhs: Option<ConstItemRhs>,
37743774
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
37753775
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
197197
id,
198198
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
199199
|this| {
200-
let ty = this
201-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
200+
let ty = this.lower_fn_ret_ty_or_unit(
201+
ty,
202+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
203+
);
202204
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), span);
203205
(ty, rhs)
204206
},
@@ -817,8 +819,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
817819
i.id,
818820
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
819821
|this| {
820-
let ty = this
821-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
822+
let ty = this.lower_fn_ret_ty_or_unit(
823+
ty,
824+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
825+
);
822826
let rhs = rhs
823827
.as_ref()
824828
.map(|rhs| this.lower_const_item_rhs(attrs, Some(rhs), i.span));
@@ -1029,8 +1033,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
10291033
i.id,
10301034
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
10311035
|this| {
1032-
let ty = this
1033-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1036+
let ty = this.lower_fn_ret_ty_or_unit(
1037+
ty,
1038+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
1039+
);
10341040
this.lower_define_opaque(hir_id, &define_opaque);
10351041
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), i.span);
10361042
hir::ImplItemKind::Const(ty, rhs)

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18011801
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
18021802
}
18031803

1804+
fn lower_fn_ret_ty_or_unit(
1805+
&mut self,
1806+
fn_ret_ty: &FnRetTy,
1807+
ictxt: ImplTraitContext,
1808+
) -> &'hir hir::Ty<'hir> {
1809+
match fn_ret_ty {
1810+
FnRetTy::Ty(ty) => self.lower_ty(ty, ictxt),
1811+
FnRetTy::Default(span) => self.arena.alloc(self.ty_tup(*span, &[])),
1812+
}
1813+
}
1814+
18041815
/// Transforms `-> T` into `Future<Output = T>`.
18051816
fn lower_coroutine_fn_output_type_to_bound(
18061817
&mut self,
@@ -1810,15 +1821,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18101821
itctx: ImplTraitContext,
18111822
) -> hir::GenericBound<'hir> {
18121823
// Compute the `T` in `Future<Output = T>` from the return type.
1813-
let output_ty = match output {
1814-
FnRetTy::Ty(ty) => {
1815-
// Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
1816-
// `impl Future` opaque type that `async fn` implicitly
1817-
// generates.
1818-
self.lower_ty(ty, itctx)
1819-
}
1820-
FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
1821-
};
1824+
// Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
1825+
// `impl Future` opaque type that `async fn` implicitly
1826+
// generates.
1827+
let output_ty = self.lower_fn_ret_ty_or_unit(output, itctx);
18221828

18231829
// "<$assoc_ty_name = T>"
18241830
let (assoc_ty_name, trait_lang_item) = match coro {

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
511511
gate_all!(generic_const_items, "generic const items are experimental");
512512
gate_all!(guard_patterns, "guard patterns are experimental", "consider using match arm guards");
513513
gate_all!(default_field_values, "default values on fields are experimental");
514+
gate_all!(
515+
const_items_unit_type_default,
516+
"omitting type on const item declaration is experimental",
517+
"consider specifying the type explicitly"
518+
);
514519
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
515520
gate_all!(postfix_match, "postfix match is experimental");
516521
gate_all!(mut_ref, "mutable by-reference bindings are experimental");

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a> State<'a> {
4747
*ident,
4848
Some(*mutability),
4949
&ast::Generics::default(),
50-
ty,
50+
Some(ty),
5151
expr.as_deref(),
5252
vis,
5353
*safety,
@@ -87,7 +87,7 @@ impl<'a> State<'a> {
8787
ident: Ident,
8888
mutbl: Option<ast::Mutability>,
8989
generics: &ast::Generics,
90-
ty: &ast::Ty,
90+
ty: Option<&ast::Ty>,
9191
body: Option<&ast::Expr>,
9292
vis: &ast::Visibility,
9393
safety: ast::Safety,
@@ -107,8 +107,10 @@ impl<'a> State<'a> {
107107
self.word_space(leading);
108108
self.print_ident(ident);
109109
self.print_generic_params(&generics.params);
110-
self.word_space(":");
111-
self.print_type(ty);
110+
if let Some(ty) = ty {
111+
self.word_space(":");
112+
self.print_type(ty);
113+
}
112114
if body.is_some() {
113115
self.space();
114116
}
@@ -197,7 +199,7 @@ impl<'a> State<'a> {
197199
*ident,
198200
Some(*mutbl),
199201
&ast::Generics::default(),
200-
ty,
202+
Some(ty),
201203
body.as_deref(),
202204
&item.vis,
203205
ast::Safety::Default,
@@ -220,7 +222,10 @@ impl<'a> State<'a> {
220222
*ident,
221223
None,
222224
generics,
223-
ty,
225+
match ty {
226+
ast::FnRetTy::Default(_) => None,
227+
ast::FnRetTy::Ty(ty) => Some(ty),
228+
},
224229
rhs.as_ref().map(|ct| ct.expr()),
225230
&item.vis,
226231
ast::Safety::Default,
@@ -572,7 +577,10 @@ impl<'a> State<'a> {
572577
*ident,
573578
None,
574579
generics,
575-
ty,
580+
match ty {
581+
ast::FnRetTy::Default(_) => None,
582+
ast::FnRetTy::Ty(ty) => Some(ty),
583+
},
576584
rhs.as_ref().map(|ct| ct.expr()),
577585
vis,
578586
ast::Safety::Default,

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::{
44
};
55
use rustc_expand::base::{Annotatable, ExtCtxt};
66
use rustc_span::{Ident, Span, kw, sym};
7-
use thin_vec::{ThinVec, thin_vec};
7+
use thin_vec::thin_vec;
88

99
use crate::errors;
1010
use crate::util::check_builtin_macro_attribute;
@@ -42,9 +42,14 @@ pub(crate) fn expand(
4242
let stmts = thin_vec![generate_handler(ecx, ident, span, sig_span)];
4343

4444
// Generate anonymous constant serving as container for the allocator methods.
45-
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
45+
4646
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
47-
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
47+
let const_item = ecx.item_const(
48+
span,
49+
Ident::new(kw::Underscore, span),
50+
ast::FnRetTy::Default(sig_span),
51+
const_body,
52+
);
4853
let const_item = if is_stmt {
4954
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
5055
} else {

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ pub(crate) fn expand(
4646
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
4747

4848
// Generate anonymous constant serving as container for the allocator methods.
49-
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
5049
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
51-
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
50+
let const_item = ecx.item_const(
51+
span,
52+
Ident::new(kw::Underscore, span),
53+
ast::FnRetTy::Default(ty_span),
54+
const_body,
55+
);
5256
let const_item = if is_stmt {
5357
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
5458
} else {

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,8 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
389389
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
390390
));
391391

392-
let anon_constant = cx.item_const(
393-
span,
394-
Ident::new(kw::Underscore, span),
395-
cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
396-
block,
397-
);
392+
let anon_constant =
393+
cx.item_const(span, Ident::new(kw::Underscore, span), ast::FnRetTy::Default(span), block);
398394

399395
// Integrate the new item into existing module structures.
400396
let items = AstFragment::Items(smallvec![anon_constant]);

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ pub(crate) fn expand_test_or_bench(
286286
defaultness: ast::Defaultness::Final,
287287
ident: Ident::new(fn_.ident.name, sp),
288288
generics: ast::Generics::default(),
289-
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
289+
ty: ast::FnRetTy::Ty(
290+
cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
291+
),
290292
define_opaque: None,
291293
// test::TestDescAndFn {
292294
rhs: Some(ast::ConstItemRhs::Body(

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'a> ExtCtxt<'a> {
725725
&self,
726726
span: Span,
727727
ident: Ident,
728-
ty: Box<ast::Ty>,
728+
ty: ast::FnRetTy,
729729
rhs: ast::ConstItemRhs,
730730
) -> Box<ast::Item> {
731731
let defaultness = ast::Defaultness::Final;

0 commit comments

Comments
 (0)