Skip to content

Commit

Permalink
Merge #10360
Browse files Browse the repository at this point in the history
10360: headsup: matklad rewrites blocks grammar again :) r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
  • Loading branch information
bors[bot] and matklad committed Sep 26, 2021
2 parents c51a3c7 + 2bf8192 commit 009e6ce
Show file tree
Hide file tree
Showing 233 changed files with 11,762 additions and 11,343 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/hir/src/semantics.rs
Expand Up @@ -698,7 +698,7 @@ impl<'db> SemanticsImpl<'db> {
ast::ForExpr(it) => it.label(),
ast::WhileExpr(it) => it.label(),
ast::LoopExpr(it) => it.label(),
ast::EffectExpr(it) => it.label(),
ast::BlockExpr(it) => it.label(),
_ => None,
}
};
Expand Down
3 changes: 2 additions & 1 deletion crates/hir_def/src/attr.rs
Expand Up @@ -524,7 +524,8 @@ fn inner_attributes(
},
ast::Fn(it) => {
let body = it.body()?;
(body.attrs(), ast::CommentIter::from_syntax_node(body.syntax()))
let stmt_list = body.stmt_list()?;
(stmt_list.attrs(), ast::CommentIter::from_syntax_node(body.syntax()))
},
ast::Impl(it) => {
let assoc_item_list = it.assoc_item_list()?;
Expand Down
40 changes: 17 additions & 23 deletions crates/hir_def/src/body/lower.rs
Expand Up @@ -245,43 +245,37 @@ impl ExprCollector<'_> {

self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
}
ast::Expr::EffectExpr(e) => match e.effect() {
ast::Effect::Try(_) => {
let body = self.collect_block_opt(e.block_expr());
ast::Expr::BlockExpr(e) => match e.modifier() {
Some(ast::BlockModifier::Try(_)) => {
let body = self.collect_block(e);
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
}
ast::Effect::Unsafe(_) => {
let body = self.collect_block_opt(e.block_expr());
Some(ast::BlockModifier::Unsafe(_)) => {
let body = self.collect_block(e);
self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
}
// FIXME: we need to record these effects somewhere...
ast::Effect::Label(label) => {
Some(ast::BlockModifier::Label(label)) => {
let label = self.collect_label(label);
match e.block_expr() {
Some(block) => {
let res = self.collect_block(block);
match &mut self.body.exprs[res] {
Expr::Block { label: block_label, .. } => {
*block_label = Some(label);
}
_ => unreachable!(),
}
res
let res = self.collect_block(e);
match &mut self.body.exprs[res] {
Expr::Block { label: block_label, .. } => {
*block_label = Some(label);
}
None => self.missing_expr(),
_ => unreachable!(),
}
res
}
// FIXME: we need to record these effects somewhere...
ast::Effect::Async(_) => {
let body = self.collect_block_opt(e.block_expr());
Some(ast::BlockModifier::Async(_)) => {
let body = self.collect_block(e);
self.alloc_expr(Expr::Async { body }, syntax_ptr)
}
ast::Effect::Const(_) => {
let body = self.collect_block_opt(e.block_expr());
Some(ast::BlockModifier::Const(_)) => {
let body = self.collect_block(e);
self.alloc_expr(Expr::Const { body }, syntax_ptr)
}
None => self.collect_block(e),
},
ast::Expr::BlockExpr(e) => self.collect_block(e),
ast::Expr::LoopExpr(e) => {
let label = e.label().map(|label| self.collect_label(label));
let body = self.collect_block_opt(e.loop_body());
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/lib.rs
Expand Up @@ -721,7 +721,7 @@ impl ExpandTo {

match parent.kind() {
MACRO_ITEMS | SOURCE_FILE | ITEM_LIST => ExpandTo::Items,
MACRO_STMTS | EXPR_STMT | BLOCK_EXPR => ExpandTo::Statements,
MACRO_STMTS | EXPR_STMT | STMT_LIST => ExpandTo::Statements,
MACRO_PAT => ExpandTo::Pattern,
MACRO_TYPE => ExpandTo::Type,

Expand Down
30 changes: 15 additions & 15 deletions crates/hir_ty/src/tests/method_resolution.rs
Expand Up @@ -1083,30 +1083,30 @@ fn dyn_trait_super_trait_not_in_scope() {
fn method_resolution_foreign_opaque_type() {
check_infer(
r#"
extern "C" {
type S;
fn f() -> &'static S;
}
extern "C" {
type S;
fn f() -> &'static S;
}
impl S {
fn foo(&self) -> bool {
true
}
}
impl S {
fn foo(&self) -> bool {
true
}
}
fn test() {
let s = unsafe { f() };
s.foo();
}
"#,
fn test() {
let s = unsafe { f() };
s.foo();
}
"#,
expect![[r#"
75..79 'self': &S
89..109 '{ ... }': bool
99..103 'true': bool
123..167 '{ ...o(); }': ()
133..134 's': &S
137..151 'unsafe { f() }': &S
144..151 '{ f() }': &S
137..151 'unsafe { f() }': &S
146..147 'f': fn f() -> &S
146..149 'f()': &S
157..158 's': &S
Expand Down
74 changes: 37 additions & 37 deletions crates/hir_ty/src/tests/simple.rs
Expand Up @@ -352,15 +352,15 @@ unsafe fn baz(u: MyUnion) {
71..89 'MyUnio...o: 0 }': MyUnion
86..87 '0': u32
95..113 'unsafe...(u); }': ()
102..113 '{ baz(u); }': ()
95..113 'unsafe...(u); }': ()
104..107 'baz': fn baz(MyUnion)
104..110 'baz(u)': ()
108..109 'u': MyUnion
122..123 'u': MyUnion
126..146 'MyUnio... 0.0 }': MyUnion
141..144 '0.0': f32
152..170 'unsafe...(u); }': ()
159..170 '{ baz(u); }': ()
152..170 'unsafe...(u); }': ()
161..164 'baz': fn baz(MyUnion)
161..167 'baz(u)': ()
165..166 'u': MyUnion
Expand Down Expand Up @@ -1914,41 +1914,41 @@ fn fn_pointer_return() {
}

#[test]
fn effects_smoke_test() {
fn block_modifiers_smoke_test() {
check_infer(
r#"
//- minicore: future
async fn main() {
let x = unsafe { 92 };
let y = async { async { () }.await };
let z = try { () };
let w = const { 92 };
let t = 'a: { 92 };
}
//- minicore: future
async fn main() {
let x = unsafe { 92 };
let y = async { async { () }.await };
let z = try { () };
let w = const { 92 };
let t = 'a: { 92 };
}
"#,
expect![[r#"
16..162 '{ ...2 }; }': ()
26..27 'x': i32
30..43 'unsafe { 92 }': i32
37..43 '{ 92 }': i32
30..43 'unsafe { 92 }': i32
39..41 '92': i32
53..54 'y': impl Future<Output = ()>
57..85 'async ...wait }': ()
57..85 'async ...wait }': impl Future<Output = ()>
63..85 '{ asyn...wait }': ()
65..77 'async { () }': ()
65..77 'async { () }': impl Future<Output = ()>
65..83 'async ....await': ()
71..77 '{ () }': ()
73..75 '()': ()
95..96 'z': {unknown}
99..109 'try { () }': ()
99..109 'try { () }': {unknown}
103..109 '{ () }': ()
105..107 '()': ()
119..120 'w': i32
123..135 'const { 92 }': i32
129..135 '{ 92 }': i32
123..135 'const { 92 }': i32
131..133 '92': i32
145..146 't': i32
153..159 '{ 92 }': i32
149..159 ''a: { 92 }': i32
155..157 '92': i32
"#]],
)
Expand Down Expand Up @@ -2104,32 +2104,32 @@ fn infer_labelled_break_with_val() {
fn infer_labelled_block_break_with_val() {
check_infer(
r#"
fn default<T>() -> T { loop {} }
fn foo() {
let _x = 'outer: {
let inner = 'inner: {
let i = default();
if (break 'outer i) {
break 'inner 5i8;
} else if true {
break 'inner 6;
}
break 'inner 'innermost: { 0 };
42
};
break 'outer inner < 8;
};
}
"#,
fn default<T>() -> T { loop {} }
fn foo() {
let _x = 'outer: {
let inner = 'inner: {
let i = default();
if (break 'outer i) {
break 'inner 5i8;
} else if true {
break 'inner 6;
}
break 'inner 'innermost: { 0 };
42
};
break 'outer inner < 8;
};
}
"#,
expect![[r#"
21..32 '{ loop {} }': T
23..30 'loop {}': !
28..30 '{}': ()
42..381 '{ ... }; }': ()
52..54 '_x': bool
65..378 '{ ... }': bool
57..378 ''outer... }': bool
79..84 'inner': i8
95..339 '{ ... }': i8
87..339 ''inner... }': i8
113..114 'i': bool
117..124 'default': fn default<bool>() -> bool
117..126 'default()': bool
Expand All @@ -2145,7 +2145,7 @@ fn infer_labelled_block_break_with_val() {
241..255 'break 'inner 6': !
254..255 '6': i8
283..313 'break ... { 0 }': !
308..313 '{ 0 }': i8
296..313 ''inner... { 0 }': i8
310..311 '0': i8
327..329 '42': i8
349..371 'break ...er < 8': !
Expand Down
25 changes: 15 additions & 10 deletions crates/ide/src/highlight_related.rs
Expand Up @@ -164,8 +164,8 @@ fn highlight_exit_points(
match anc {
ast::Fn(fn_) => hl(sema, fn_.body().map(ast::Expr::BlockExpr)),
ast::ClosureExpr(closure) => hl(sema, closure.body()),
ast::EffectExpr(effect) => if matches!(effect.effect(), ast::Effect::Async(_) | ast::Effect::Try(_)| ast::Effect::Const(_)) {
hl(sema, effect.block_expr().map(ast::Expr::BlockExpr))
ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
hl(sema, Some(block_expr.into()))
} else {
continue;
},
Expand All @@ -180,7 +180,7 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
fn hl(
token: Option<SyntaxToken>,
label: Option<ast::Label>,
body: Option<ast::BlockExpr>,
body: Option<ast::StmtList>,
) -> Option<Vec<HighlightedRange>> {
let mut highlights = Vec::new();
let range = cover_range(
Expand All @@ -204,7 +204,7 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()),
ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()),
ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()),
ast::EffectExpr(b) => Some(b.label().and_then(|it| it.lifetime())?),
ast::BlockExpr(b) => Some(b.label().and_then(|it| it.lifetime())?),
_ => return None,
}
};
Expand All @@ -218,16 +218,16 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
for anc in token.ancestors().flat_map(ast::Expr::cast) {
return match anc {
ast::Expr::LoopExpr(l) if label_matches(l.label()) => {
hl(l.loop_token(), l.label(), l.loop_body())
hl(l.loop_token(), l.label(), l.loop_body().and_then(|it| it.stmt_list()))
}
ast::Expr::ForExpr(f) if label_matches(f.label()) => {
hl(f.for_token(), f.label(), f.loop_body())
hl(f.for_token(), f.label(), f.loop_body().and_then(|it| it.stmt_list()))
}
ast::Expr::WhileExpr(w) if label_matches(w.label()) => {
hl(w.while_token(), w.label(), w.loop_body())
hl(w.while_token(), w.label(), w.loop_body().and_then(|it| it.stmt_list()))
}
ast::Expr::EffectExpr(e) if e.label().is_some() && label_matches(e.label()) => {
hl(None, e.label(), e.block_expr())
ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => {
hl(None, e.label(), e.stmt_list())
}
_ => continue,
};
Expand Down Expand Up @@ -258,7 +258,12 @@ fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
return match_ast! {
match anc {
ast::Fn(fn_) => hl(fn_.async_token(), fn_.body().map(ast::Expr::BlockExpr)),
ast::EffectExpr(effect) => hl(effect.async_token(), effect.block_expr().map(ast::Expr::BlockExpr)),
ast::BlockExpr(block_expr) => {
if block_expr.async_token().is_none() {
continue;
}
hl(block_expr.async_token(), Some(block_expr.into()))
},
ast::ClosureExpr(closure) => hl(closure.async_token(), closure.body()),
_ => continue,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/hover/render.rs
Expand Up @@ -86,8 +86,8 @@ pub(super) fn try_expr(
ast::Fn(fn_) => sema.to_def(&fn_)?.ret_type(sema.db),
ast::Item(__) => return None,
ast::ClosureExpr(closure) => sema.type_of_expr(&closure.body()?)?.original,
ast::EffectExpr(effect) => if matches!(effect.effect(), ast::Effect::Async(_) | ast::Effect::Try(_)| ast::Effect::Const(_)) {
sema.type_of_expr(&effect.block_expr()?.into())?.original
ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
sema.type_of_expr(&block_expr.into())?.original
} else {
continue;
},
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/join_lines.rs
Expand Up @@ -212,7 +212,7 @@ fn remove_newline(
}

fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
let block_expr = ast::BlockExpr::cast(token.parent()?)?;
let block_expr = ast::BlockExpr::cast(token.ancestors().nth(1)?)?;
if !block_expr.is_standalone() {
return None;
}
Expand Down

0 comments on commit 009e6ce

Please sign in to comment.