Skip to content

Commit

Permalink
first version of super let syntax support
Browse files Browse the repository at this point in the history
smoke tests of the new syntax

move new_temp_lifetime into query

restore classic scoping rules
  • Loading branch information
dingxiangfei2009 committed Dec 17, 2023
1 parent 2f19122 commit d3a6b64
Show file tree
Hide file tree
Showing 27 changed files with 818 additions and 54 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,20 +1075,23 @@ pub enum LocalKind {
/// Local declaration with an initializer and an `else` clause.
/// Example: `let Some(x) = y else { return };`
InitElse(P<Expr>, P<Block>),
/// Local declaration with an initializer living through the temporary lifetime.
/// Example: `super let x = y;`
Super(P<Expr>),
}

impl LocalKind {
pub fn init(&self) -> Option<&Expr> {
match self {
Self::Decl => None,
Self::Init(i) | Self::InitElse(i, _) => Some(i),
Self::Init(i) | Self::InitElse(i, _) | Self::Super(i) => Some(i),
}
}

pub fn init_else_opt(&self) -> Option<(&Expr, Option<&Block>)> {
match self {
Self::Decl => None,
Self::Init(init) => Some((init, None)),
Self::Init(init) | Self::Super(init) => Some((init, None)),
Self::InitElse(init, els) => Some((init, Some(els))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
visit_opt(ty, |ty| vis.visit_ty(ty));
match kind {
LocalKind::Decl => {}
LocalKind::Init(init) => {
LocalKind::Init(init) | LocalKind::Super(init) => {
vis.visit_expr(init);
}
LocalKind::InitElse(init, els) => {
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let span = self.lower_span(l.span);
let source = hir::LocalSource::Normal;
self.lower_attrs(hir_id, &l.attrs);
self.arena.alloc(hir::Local { hir_id, ty, pat, init, els, span, source })
self.arena.alloc(hir::Local {
hir_id,
ty,
pat,
init,
els,
span,
source,
is_super: matches!(l.kind, LocalKind::Super(..)),
})
}

fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
source,
span: self.lower_span(span),
ty: None,
is_super: false,
};
self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local)))
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,9 @@ impl<'a> State<'a> {
self.print_outer_attributes(&loc.attrs);
self.space_if_not_bol();
self.ibox(INDENT_UNIT);
if matches!(loc.kind, ast::LocalKind::Super(..)) {
self.word_nbsp("super")
}
self.word_nbsp("let");

self.ibox(INDENT_UNIT);
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 @@ -532,6 +532,8 @@ declare_features! (
(unstable, never_type, "1.13.0", Some(35121)),
/// Allows diverging expressions to fall back to `!` rather than `()`.
(unstable, never_type_fallback, "1.41.0", Some(65992)),
/// Allows new temporary lifetime rules
(unstable, new_temp_lifetime, "1.72.0", Some(99999)),
/// Allows `#![no_core]`.
(unstable, no_core, "1.3.0", Some(29639)),
/// Allows the use of `no_sanitize` attribute.
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 @@ -1227,6 +1227,7 @@ pub struct Local<'hir> {
/// Else block for a `let...else` binding.
pub els: Option<&'hir Block<'hir>>,
pub hir_id: HirId,
pub is_super: bool,
pub span: Span,
/// Can be `ForLoopDesugar` if the `let` statement is part of a `for` loop
/// desugaring. Otherwise will be `Normal`.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod entry;
pub mod intrinsic;
pub mod intrinsicck;
mod region;
mod scope_map;
pub mod wfcheck;

pub use check::check_abi;
Expand Down Expand Up @@ -104,6 +105,7 @@ use crate::util::common::indenter;

use self::compare_impl_item::collect_return_position_impl_trait_in_trait_tys;
use self::region::region_scope_tree;
use self::scope_map::body_scope_map;

pub fn provide(providers: &mut Providers) {
wfcheck::provide(providers);
Expand All @@ -114,6 +116,7 @@ pub fn provide(providers: &mut Providers) {
collect_return_position_impl_trait_in_trait_tys,
compare_impl_const: compare_impl_item::compare_impl_const_raw,
check_coroutine_obligations: check::check_coroutine_obligations,
body_scope_map,
..*providers
};
}
Expand Down

0 comments on commit d3a6b64

Please sign in to comment.