Skip to content

Commit 044c7e9

Browse files
authored
Unrolled build for #153211
Rollup merge of #153211 - lnicola:sync-from-ra, r=lnicola `rust-analyzer` subtree update Subtree update of `rust-analyzer` to rust-lang/rust-analyzer@fbc7b76. Created using https://github.com/rust-lang/josh-sync. r? @ghost
2 parents 1d113d2 + 7a22108 commit 044c7e9

File tree

18 files changed

+255
-65
lines changed

18 files changed

+255
-65
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,7 @@ impl<'db> CastCheck<'db> {
328328
//
329329
// Note that trait upcasting goes through a different mechanism (`coerce_unsized`)
330330
// and is unaffected by this check.
331-
(Some(src_principal), Some(dst_principal)) => {
332-
if src_principal == dst_principal {
333-
return Ok(());
334-
}
335-
331+
(Some(src_principal), Some(_)) => {
336332
// We need to reconstruct trait object types.
337333
// `m_src` and `m_dst` won't work for us here because they will potentially
338334
// contain wrappers, which we do not care about.

src/tools/rust-analyzer/crates/ide-assists/src/handlers/fix_visibility.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use hir::{HasSource, HasVisibility, ModuleDef, PathResolution, ScopeDef, db::Hir
22
use ide_db::FileId;
33
use syntax::{
44
AstNode, TextRange,
5-
ast::{self, HasVisibility as _, edit_in_place::HasVisibilityEdit, make},
5+
ast::{self, HasVisibility as _, syntax_factory::SyntaxFactory},
66
};
77

88
use crate::{AssistContext, AssistId, Assists};
@@ -59,10 +59,12 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext<'_>)
5959

6060
let (vis_owner, target, target_file, target_name) = target_data_for_def(ctx.db(), def)?;
6161

62+
let make = SyntaxFactory::without_mappings();
63+
6264
let missing_visibility = if current_module.krate(ctx.db()) == target_module.krate(ctx.db()) {
63-
make::visibility_pub_crate()
65+
make.visibility_pub_crate()
6466
} else {
65-
make::visibility_pub()
67+
make.visibility_pub()
6668
};
6769

6870
let assist_label = match target_name {
@@ -75,15 +77,36 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext<'_>)
7577
}
7678
};
7779

78-
acc.add(AssistId::quick_fix("fix_visibility"), assist_label, target, |edit| {
79-
edit.edit_file(target_file);
80-
81-
let vis_owner = edit.make_mut(vis_owner);
82-
vis_owner.set_visibility(Some(missing_visibility.clone_for_update()));
80+
acc.add(AssistId::quick_fix("fix_visibility"), assist_label, target, |builder| {
81+
let mut editor = builder.make_editor(vis_owner.syntax());
82+
83+
if let Some(current_visibility) = vis_owner.visibility() {
84+
editor.replace(current_visibility.syntax(), missing_visibility.syntax());
85+
} else {
86+
let vis_before = vis_owner
87+
.syntax()
88+
.children_with_tokens()
89+
.find(|it| {
90+
!matches!(
91+
it.kind(),
92+
syntax::SyntaxKind::WHITESPACE
93+
| syntax::SyntaxKind::COMMENT
94+
| syntax::SyntaxKind::ATTR
95+
)
96+
})
97+
.unwrap_or_else(|| vis_owner.syntax().first_child_or_token().unwrap());
98+
99+
editor.insert_all(
100+
syntax::syntax_editor::Position::before(vis_before),
101+
vec![missing_visibility.syntax().clone().into(), make.whitespace(" ").into()],
102+
);
103+
}
83104

84-
if let Some((cap, vis)) = ctx.config.snippet_cap.zip(vis_owner.visibility()) {
85-
edit.add_tabstop_before(cap, vis);
105+
if let Some(cap) = ctx.config.snippet_cap {
106+
editor.add_annotation(missing_visibility.syntax(), builder.make_tabstop_before(cap));
86107
}
108+
109+
builder.add_file_edits(target_file, editor);
87110
})
88111
}
89112

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_derive.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use syntax::{
22
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
33
T,
4-
ast::{self, AstNode, HasAttrs, edit::IndentLevel, make},
4+
ast::{self, AstNode, HasAttrs, edit::IndentLevel, syntax_factory::SyntaxFactory},
55
syntax_editor::{Element, Position},
66
};
77

@@ -42,13 +42,15 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
4242
};
4343

4444
acc.add(AssistId::generate("generate_derive"), "Add `#[derive]`", target, |edit| {
45+
let make = SyntaxFactory::without_mappings();
46+
4547
match derive_attr {
4648
None => {
47-
let derive = make::attr_outer(make::meta_token_tree(
48-
make::ext::ident_path("derive"),
49-
make::token_tree(T!['('], vec![]).clone_for_update(),
50-
))
51-
.clone_for_update();
49+
let derive =
50+
make.attr_outer(make.meta_token_tree(
51+
make.ident_path("derive"),
52+
make.token_tree(T!['('], vec![]),
53+
));
5254

5355
let mut editor = edit.make_editor(nominal.syntax());
5456
let indent = IndentLevel::from_node(nominal.syntax());
@@ -57,11 +59,12 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
5759
.children_with_tokens()
5860
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
5961
.map_or(Position::first_child_of(nominal.syntax()), Position::before);
62+
6063
editor.insert_all(
6164
after_attrs_and_comments,
6265
vec![
6366
derive.syntax().syntax_element(),
64-
make::tokens::whitespace(&format!("\n{indent}")).syntax_element(),
67+
make.whitespace(&format!("\n{indent}")).syntax_element(),
6568
],
6669
);
6770

@@ -72,7 +75,9 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
7275
.expect("failed to get token tree out of Meta")
7376
.r_paren_token()
7477
.expect("make::attr_outer was expected to have a R_PAREN");
78+
7579
let tabstop_before = edit.make_tabstop_before(cap);
80+
7681
editor.add_annotation(delimiter, tabstop_before);
7782
edit.add_file_edits(ctx.vfs_file_id(), editor);
7883
}

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
5757
let if_exprs = successors(Some(if_expr.clone()), |expr| match expr.else_branch()? {
5858
ast::ElseBranch::IfExpr(expr) => Some(expr),
5959
ast::ElseBranch::Block(block) => {
60-
let block = unwrap_trivial_block(block).clone_for_update();
60+
let block = unwrap_trivial_block(block);
6161
else_block = Some(block.reset_indent().indent(IndentLevel(1)));
6262
None
6363
}
@@ -91,7 +91,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
9191
guard
9292
};
9393

94-
let body = if_expr.then_branch()?.clone_for_update().indent(IndentLevel(1));
94+
let body = if_expr.then_branch()?.indent(IndentLevel(1));
9595
cond_bodies.push((cond, guard, body));
9696
}
9797

@@ -114,7 +114,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
114114
let make_match_arm =
115115
|(pat, guard, body): (_, Option<ast::Expr>, ast::BlockExpr)| {
116116
// Dedent from original position, then indent for match arm
117-
let body = body.dedent(indent).indent(IndentLevel::single());
117+
let body = body.dedent(indent);
118118
let body = unwrap_trivial_block(body);
119119
match (pat, guard.map(|it| make.match_guard(it))) {
120120
(Some(pat), guard) => make.match_arm(pat, guard, body),
@@ -127,8 +127,8 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
127127
}
128128
};
129129
let arms = cond_bodies.into_iter().map(make_match_arm).chain([else_arm]);
130-
let match_expr =
131-
make.expr_match(scrutinee_to_be_expr, make.match_arm_list(arms)).indent(indent);
130+
let expr = scrutinee_to_be_expr.reset_indent();
131+
let match_expr = make.expr_match(expr, make.match_arm_list(arms)).indent(indent);
132132
match_expr.into()
133133
};
134134

@@ -246,7 +246,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
246246
first_arm.guard(),
247247
second_arm.guard(),
248248
)?;
249-
let scrutinee = match_expr.expr()?;
249+
let scrutinee = match_expr.expr()?.reset_indent();
250250
let guard = guard.and_then(|it| it.condition());
251251

252252
let let_ = match &if_let_pat {
@@ -293,10 +293,8 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
293293
} else {
294294
condition
295295
};
296-
let then_expr =
297-
then_expr.clone_for_update().reset_indent().indent(IndentLevel::single());
298-
let else_expr =
299-
else_expr.clone_for_update().reset_indent().indent(IndentLevel::single());
296+
let then_expr = then_expr.reset_indent();
297+
let else_expr = else_expr.reset_indent();
300298
let then_block = make_block_expr(then_expr);
301299
let else_expr = if is_empty_expr(&else_expr) { None } else { Some(else_expr) };
302300
let if_let_expr = make
@@ -956,7 +954,9 @@ fn foo(x: Result<i32, ()>) {
956954
r#"
957955
fn main() {
958956
if true {
959-
$0if let Ok(rel_path) = path.strip_prefix(root_path) {
957+
$0if let Ok(rel_path) = path.strip_prefix(root_path)
958+
.and(x)
959+
{
960960
let rel_path = RelativePathBuf::from_path(rel_path)
961961
.ok()?;
962962
Some((*id, rel_path))
@@ -971,7 +971,8 @@ fn main() {
971971
r#"
972972
fn main() {
973973
if true {
974-
match path.strip_prefix(root_path) {
974+
match path.strip_prefix(root_path)
975+
.and(x) {
975976
Ok(rel_path) => {
976977
let rel_path = RelativePathBuf::from_path(rel_path)
977978
.ok()?;
@@ -993,7 +994,9 @@ fn main() {
993994
r#"
994995
fn main() {
995996
if true {
996-
$0if let Ok(rel_path) = path.strip_prefix(root_path) {
997+
$0if let Ok(rel_path) = path.strip_prefix(root_path)
998+
.and(x)
999+
{
9971000
Foo {
9981001
x: 1
9991002
}
@@ -1008,7 +1011,8 @@ fn main() {
10081011
r#"
10091012
fn main() {
10101013
if true {
1011-
match path.strip_prefix(root_path) {
1014+
match path.strip_prefix(root_path)
1015+
.and(x) {
10121016
Ok(rel_path) => {
10131017
Foo {
10141018
x: 1
@@ -1023,7 +1027,33 @@ fn main() {
10231027
}
10241028
}
10251029
"#,
1026-
)
1030+
);
1031+
1032+
check_assist(
1033+
replace_if_let_with_match,
1034+
r#"
1035+
fn main() {
1036+
if true {
1037+
$0if true
1038+
&& false
1039+
{
1040+
foo()
1041+
}
1042+
}
1043+
}
1044+
"#,
1045+
r#"
1046+
fn main() {
1047+
if true {
1048+
match true
1049+
&& false {
1050+
true => foo(),
1051+
false => (),
1052+
}
1053+
}
1054+
}
1055+
"#,
1056+
);
10271057
}
10281058

10291059
#[test]
@@ -1878,7 +1908,9 @@ fn foo(x: Result<i32, ()>) {
18781908
r#"
18791909
fn main() {
18801910
if true {
1881-
$0match path.strip_prefix(root_path) {
1911+
$0match path.strip_prefix(root_path)
1912+
.and(x)
1913+
{
18821914
Ok(rel_path) => Foo {
18831915
x: 2
18841916
}
@@ -1892,7 +1924,8 @@ fn main() {
18921924
r#"
18931925
fn main() {
18941926
if true {
1895-
if let Ok(rel_path) = path.strip_prefix(root_path) {
1927+
if let Ok(rel_path) = path.strip_prefix(root_path)
1928+
.and(x) {
18961929
Foo {
18971930
x: 2
18981931
}
@@ -1911,7 +1944,9 @@ fn main() {
19111944
r#"
19121945
fn main() {
19131946
if true {
1914-
$0match path.strip_prefix(root_path) {
1947+
$0match path.strip_prefix(root_path)
1948+
.and(x)
1949+
{
19151950
Ok(rel_path) => {
19161951
let rel_path = RelativePathBuf::from_path(rel_path)
19171952
.ok()?;
@@ -1929,7 +1964,8 @@ fn main() {
19291964
r#"
19301965
fn main() {
19311966
if true {
1932-
if let Ok(rel_path) = path.strip_prefix(root_path) {
1967+
if let Ok(rel_path) = path.strip_prefix(root_path)
1968+
.and(x) {
19331969
let rel_path = RelativePathBuf::from_path(rel_path)
19341970
.ok()?;
19351971
Some((*id, rel_path))

src/tools/rust-analyzer/crates/ide-completion/src/completions/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn complete_pattern(
9595
if refutable || single_variant_enum(variant.parent_enum(ctx.db)) =>
9696
{
9797
acc.add_variant_pat(ctx, pattern_ctx, None, variant, Some(name.clone()));
98-
true
98+
false
9999
}
100100
hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),
101101
hir::ModuleDef::Const(..) => refutable,

src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ fn foo() {
122122
st Record
123123
st Tuple
124124
st Unit
125-
ev TupleV
126125
bn Record {…} Record { field$1 }$0
127126
bn Tuple(…) Tuple($1)$0
128127
bn TupleV(…) TupleV($1)$0
@@ -159,8 +158,6 @@ fn foo(foo: Foo) { match foo { Foo { x: $0 } } }
159158
expect![[r#"
160159
en Bar
161160
st Foo
162-
ev Nil
163-
ev Value
164161
bn Foo {…} Foo { x$1 }$0
165162
bn Nil Nil$0
166163
bn Value Value$0
@@ -189,7 +186,6 @@ fn foo() {
189186
st Record
190187
st Tuple
191188
st Unit
192-
ev Variant
193189
bn Record {…} Record { field$1 }$0
194190
bn Tuple(…) Tuple($1)$0
195191
bn Variant Variant$0
@@ -354,6 +350,34 @@ fn func() {
354350
);
355351
}
356352

353+
#[test]
354+
fn enum_unqualified() {
355+
check_with_base_items(
356+
r#"
357+
use Enum::*;
358+
fn func() {
359+
if let $0 = unknown {}
360+
}
361+
"#,
362+
expect![[r#"
363+
ct CONST
364+
en Enum
365+
ma makro!(…) macro_rules! makro
366+
md module
367+
st Record
368+
st Tuple
369+
st Unit
370+
bn Record {…} Record { field$1 }$0
371+
bn RecordV {…} RecordV { field$1 }$0
372+
bn Tuple(…) Tuple($1)$0
373+
bn TupleV(…) TupleV($1)$0
374+
bn UnitV UnitV$0
375+
kw mut
376+
kw ref
377+
"#]],
378+
);
379+
}
380+
357381
#[test]
358382
fn completes_in_record_field_pat() {
359383
check(

0 commit comments

Comments
 (0)