Skip to content
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
9 changes: 8 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3237,6 +3237,13 @@ impl FnRetTy {
FnRetTy::Ty(ty) => ty.span,
}
}

pub fn as_non_default(&self) -> Option<&Ty> {
match self {
Self::Default(_) => None,
Self::Ty(ty) => Some(&*ty),
}
}
}

#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, Walkable)]
Expand Down Expand Up @@ -3872,7 +3879,7 @@ pub struct ConstItem {
pub defaultness: Defaultness,
pub ident: Ident,
pub generics: Generics,
pub ty: Box<Ty>,
pub ty: FnRetTy,
pub rhs: Option<ConstItemRhs>,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
}
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_ast/src/util/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ fn path_return_type(path: &ast::Path) -> Option<&ast::Ty> {
let last_segment = path.segments.last()?;
let args = last_segment.args.as_ref()?;
match &**args {
ast::GenericArgs::Parenthesized(args) => match &args.output {
ast::FnRetTy::Default(_) => None,
ast::FnRetTy::Ty(ret) => Some(ret),
},
ast::GenericArgs::Parenthesized(args) => args.output.as_non_default(),
ast::GenericArgs::AngleBracketed(_) | ast::GenericArgs::ParenthesizedElided(_) => None,
}
}
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
id,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| {
let ty = this.lower_ty_alloc(
let ty = this.lower_fn_ret_ty_or_unit(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
Expand Down Expand Up @@ -942,7 +942,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
i.id,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| {
let ty = this.lower_ty_alloc(
let ty = this.lower_fn_ret_ty_or_unit(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
i.id,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| {
let ty = this.lower_ty_alloc(
let ty = this.lower_fn_ret_ty_or_unit(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
}

fn lower_fn_ret_ty_or_unit(
&mut self,
fn_ret_ty: &FnRetTy,
ictxt: ImplTraitContext,
) -> &'hir hir::Ty<'hir> {
match fn_ret_ty {
FnRetTy::Ty(ty) => self.lower_ty_alloc(ty, ictxt),
FnRetTy::Default(span) => self.arena.alloc(self.ty_tup(*span, &[])),
}
}

/// Transforms `-> T` into `Future<Output = T>`.
fn lower_coroutine_fn_output_type_to_bound(
&mut self,
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(generic_const_items, "generic const items are experimental");
gate_all!(guard_patterns, "guard patterns are experimental", "consider using match arm guards");
gate_all!(default_field_values, "default values on fields are experimental");
gate_all!(
const_items_unit_type_default,
"omitting type on const item declaration is experimental",
"consider specifying the type explicitly"
);
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
gate_all!(postfix_match, "postfix match is experimental");
gate_all!(mut_ref, "mutable by-reference bindings are experimental");
Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a> State<'a> {
*ident,
Some(*mutability),
&ast::Generics::default(),
ty,
Some(ty),
expr.as_deref(),
vis,
*safety,
Expand Down Expand Up @@ -87,7 +87,7 @@ impl<'a> State<'a> {
ident: Ident,
mutbl: Option<ast::Mutability>,
generics: &ast::Generics,
ty: &ast::Ty,
ty: Option<&ast::Ty>,
body: Option<&ast::Expr>,
vis: &ast::Visibility,
safety: ast::Safety,
Expand All @@ -107,8 +107,10 @@ impl<'a> State<'a> {
self.word_space(leading);
self.print_ident(ident);
self.print_generic_params(&generics.params);
self.word_space(":");
self.print_type(ty);
if let Some(ty) = ty {
self.word_space(":");
self.print_type(ty);
}
if body.is_some() {
self.space();
}
Expand Down Expand Up @@ -197,7 +199,7 @@ impl<'a> State<'a> {
*ident,
Some(*mutbl),
&ast::Generics::default(),
ty,
Some(ty),
body.as_deref(),
&item.vis,
ast::Safety::Default,
Expand Down Expand Up @@ -228,7 +230,7 @@ impl<'a> State<'a> {
*ident,
None,
generics,
ty,
ty.as_non_default(),
rhs.as_ref().map(|ct| ct.expr()),
&item.vis,
ast::Safety::Default,
Expand Down Expand Up @@ -580,7 +582,7 @@ impl<'a> State<'a> {
*ident,
None,
generics,
ty,
ty.as_non_default(),
rhs.as_ref().map(|ct| ct.expr()),
vis,
ast::Safety::Default,
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_ast::{
};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::{Ident, Span, kw, sym};
use thin_vec::{ThinVec, thin_vec};
use thin_vec::thin_vec;

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

// Generate anonymous constant serving as container for the allocator methods.
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));

let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
let const_item = ecx.item_const(
span,
Ident::new(kw::Underscore, span),
ast::FnRetTy::Default(sig_span),
const_body,
);
let const_item = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down Expand Up @@ -90,7 +95,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
contract: None,
body,
define_opaque: None,
eii_impls: ThinVec::new(),
eii_impls: thin_vec![],
}));

let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/eii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn generate_default_impl(
ecx.item_const(
span,
underscore,
unit,
ast::FnRetTy::Ty(unit),
ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts))),
)
};
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ pub(crate) fn expand(
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();

// Generate anonymous constant serving as container for the allocator methods.
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
let const_item = ecx.item_const(
span,
Ident::new(kw::Underscore, span),
ast::FnRetTy::Default(ty_span),
const_body,
);
let const_item = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,8 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
));

let anon_constant = cx.item_const(
span,
Ident::new(kw::Underscore, span),
cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
block,
);
let anon_constant =
cx.item_const(span, Ident::new(kw::Underscore, span), ast::FnRetTy::Default(span), block);

// Integrate the new item into existing module structures.
let items = AstFragment::Items(smallvec![anon_constant]);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ pub(crate) fn expand_test_or_bench(
defaultness: ast::Defaultness::Final,
ident: Ident::new(fn_.ident.name, sp),
generics: ast::Generics::default(),
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
ty: ast::FnRetTy::Ty(
cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
),
define_opaque: None,
// test::TestDescAndFn {
rhs: Some(ast::ConstItemRhs::Body(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ impl<'a> ExtCtxt<'a> {
&self,
span: Span,
ident: Ident,
ty: Box<ast::Ty>,
ty: ast::FnRetTy,
rhs: ast::ConstItemRhs,
) -> Box<ast::Item> {
let defaultness = ast::Defaultness::Final;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ declare_features! (
(unstable, const_destruct, "1.85.0", Some(133214)),
/// Allows `for _ in _` loops in const contexts.
(unstable, const_for, "1.56.0", Some(87575)),
/// Allows to omit `: ()` type in `const FOO: () = ...` similarly to `-> ()` in functions.
(unstable, const_items_unit_type_default, "CURRENT_RUSTC_VERSION", Some(149226)),
/// Be more precise when looking for live drops in a const context.
(unstable, const_precise_live_drops, "1.46.0", Some(73255)),
/// Allows `impl const Trait for T` syntax.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ impl AttributeExt for Attribute {
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
Attribute::Parsed(AttributeKind::Coroutine(span)) => *span,
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
}
}
Expand Down
Loading
Loading