Skip to content

Commit 7dad024

Browse files
giacomocavalierilpil
authored andcommitted
use correct location for use statements
1 parent aee01bf commit 7dad024

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

compiler-core/src/ast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,7 +3569,7 @@ impl UntypedStatement {
35693569
match self {
35703570
Statement::Expression(expression) => expression.location(),
35713571
Statement::Assignment(assignment) => assignment.location,
3572-
Statement::Use(use_) => use_.location,
3572+
Statement::Use(use_) => use_.location.merge(&use_.call.location()),
35733573
Statement::Assert(assert) => assert.location,
35743574
}
35753575
}
@@ -3598,7 +3598,9 @@ impl TypedStatement {
35983598
match self {
35993599
Statement::Expression(expression) => expression.location(),
36003600
Statement::Assignment(assignment) => assignment.location,
3601-
Statement::Use(use_) => use_.location,
3601+
// A use statement covers the entire block: `use_.location` covers
3602+
// just the use's first line and not what comes after it.
3603+
Statement::Use(use_) => use_.location.merge(&use_.call.location()),
36023604
Statement::Assert(assert) => assert.location,
36033605
}
36043606
}

compiler-core/src/language_server/code_action.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8626,12 +8626,7 @@ impl<'a> ExtractFunction<'a> {
86268626
let location = statements
86278627
.first()
86288628
.location()
8629-
.merge(&match &statements.last() {
8630-
ast::Statement::Expression(expression) => expression.location(),
8631-
ast::Statement::Assignment(assignment) => assignment.location,
8632-
ast::Statement::Assert(assert) => assert.location,
8633-
ast::Statement::Use(use_) => use_.call.location(),
8634-
});
8629+
.merge(&statements.last().location());
86358630

86368631
self.extract_code_in_tail_position(
86378632
*full_location,
@@ -8972,12 +8967,7 @@ impl<'ast> ast::visit::Visit<'ast> for ExtractFunction<'ast> {
89728967
statements: &'ast [TypedStatement],
89738968
) {
89748969
let last_statement_location = self.last_statement_location;
8975-
self.last_statement_location = statements.last().map(|last| match last {
8976-
ast::Statement::Expression(_)
8977-
| ast::Statement::Assignment(_)
8978-
| ast::Statement::Assert(_) => last.location(),
8979-
ast::Statement::Use(use_) => last.location().merge(&use_.call.location()),
8980-
});
8970+
self.last_statement_location = statements.last().map(|last| last.location());
89818971

89828972
ast::visit::visit_typed_expr_block(self, location, statements);
89838973

@@ -9002,12 +8992,7 @@ impl<'ast> ast::visit::Visit<'ast> for ExtractFunction<'ast> {
90028992
}
90038993

90048994
fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) {
9005-
let statement_location = match statement {
9006-
ast::Statement::Expression(expression) => expression.location(),
9007-
ast::Statement::Assignment(assignment) => assignment.location,
9008-
ast::Statement::Assert(assert) => assert.location,
9009-
ast::Statement::Use(use_) => use_.location.merge(&use_.call.location()),
9010-
};
8995+
let statement_location = statement.location();
90118996

90128997
if self.can_extract(statement_location) {
90138998
let is_in_tail_position =

0 commit comments

Comments
 (0)