⬆️ rust-analyzer

This commit is contained in:
Laurențiu Nicola 2023-03-20 08:31:01 +02:00
parent 544b4cfe4d
commit dbf04a5ee2
106 changed files with 2219 additions and 609 deletions

View file

@ -297,6 +297,7 @@ impl Foo<str> {}
//- /lib.rs crate:main deps:core
fn foo(_: bool$0) {{}}
//- /libcore.rs crate:core
#![rustc_coherence_is_core]
#[lang = "bool"]
impl bool {}
//^^^^

View file

@ -55,7 +55,7 @@ pub(crate) fn goto_type_definition(
ty
} else {
let record_field = ast::RecordPatField::for_field_name_ref(&it)?;
sema.resolve_record_pat_field(&record_field)?.ty(db)
sema.resolve_record_pat_field(&record_field)?.1
}
},
_ => return None,

View file

@ -31,19 +31,31 @@ pub(super) fn hints(
return None;
}
// These inherit from the inner expression which would result in duplicate hints
if let ast::Expr::ParenExpr(_)
| ast::Expr::IfExpr(_)
| ast::Expr::BlockExpr(_)
| ast::Expr::MatchExpr(_) = expr
{
// ParenExpr resolve to their contained expressions HIR so they will dupe these hints
if let ast::Expr::ParenExpr(_) = expr {
return None;
}
if let ast::Expr::BlockExpr(b) = expr {
if !b.is_standalone() {
return None;
}
}
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
let desc_expr = descended.as_ref().unwrap_or(expr);
let adjustments = sema.expr_adjustments(desc_expr).filter(|it| !it.is_empty())?;
if let ast::Expr::BlockExpr(_) | ast::Expr::IfExpr(_) | ast::Expr::MatchExpr(_) = desc_expr {
if let [Adjustment { kind: Adjust::Deref(_), source, .. }, Adjustment { kind: Adjust::Borrow(_), source: _, target }] =
&*adjustments
{
// Don't show unnecessary reborrows for these, they will just repeat the inner ones again
if source == target {
return None;
}
}
}
let (postfix, needs_outer_parens, needs_inner_parens) =
mode_and_needs_parens_for_adjustment_hints(expr, config.adjustment_hints_mode);
@ -67,6 +79,7 @@ pub(super) fn hints(
for Adjustment { source, target, kind } in iter {
if source == target {
cov_mark::hit!(same_type_adjustment);
continue;
}
@ -251,7 +264,7 @@ mod tests {
check_with_config(
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
r#"
//- minicore: coerce_unsized, fn
//- minicore: coerce_unsized, fn, eq
fn main() {
let _: u32 = loop {};
//^^^^^^^<never-to-any>
@ -332,7 +345,7 @@ fn main() {
loop {}
//^^^^^^^<never-to-any>
};
let _: &mut [u32] = match () { () => &mut [] }
let _: &mut [u32] = match () { () => &mut [] };
//^^^^^^^<unsize>
//^^^^^^^&mut $
//^^^^^^^*
@ -341,6 +354,12 @@ fn main() {
//^^^^^^^^^^<unsize>
//^^^^^^^^^^&mut $
//^^^^^^^^^^*
() == ();
// ^^&
// ^^&
(()) == {()};
// ^^&
// ^^^^&
}
#[derive(Copy, Clone)]
@ -363,7 +382,7 @@ impl Struct {
..DISABLED_CONFIG
},
r#"
//- minicore: coerce_unsized, fn
//- minicore: coerce_unsized, fn, eq
fn main() {
Struct.consume();
@ -419,7 +438,7 @@ fn main() {
loop {}
//^^^^^^^.<never-to-any>
};
let _: &mut [u32] = match () { () => &mut [] }
let _: &mut [u32] = match () { () => &mut [] };
//^^^^^^^(
//^^^^^^^)
//^^^^^^^.*
@ -432,6 +451,12 @@ fn main() {
//^^^^^^^^^^.*
//^^^^^^^^^^.&mut
//^^^^^^^^^^.<unsize>
() == ();
// ^^.&
// ^^.&
(()) == {()};
// ^^.&
// ^^^^.&
}
#[derive(Copy, Clone)]
@ -499,6 +524,7 @@ fn main() {
#[test]
fn never_to_never_is_never_shown() {
cov_mark::check!(same_type_adjustment);
check_with_config(
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
r#"

View file

@ -435,7 +435,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3386..3394,
range: 3415..3423,
},
),
tooltip: "",
@ -448,7 +448,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3418..3422,
range: 3447..3451,
},
),
tooltip: "",
@ -468,7 +468,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3386..3394,
range: 3415..3423,
},
),
tooltip: "",
@ -481,7 +481,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3418..3422,
range: 3447..3451,
},
),
tooltip: "",
@ -501,7 +501,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3386..3394,
range: 3415..3423,
},
),
tooltip: "",
@ -514,7 +514,7 @@ fn main() {
file_id: FileId(
1,
),
range: 3418..3422,
range: 3447..3451,
},
),
tooltip: "",

View file

@ -16,7 +16,7 @@ use stdx::format_to;
use syntax::{
algo,
ast::{self, HasArgList},
match_ast, AstNode, Direction, SyntaxToken, TextRange, TextSize,
match_ast, AstNode, Direction, SyntaxElementChildren, SyntaxToken, TextRange, TextSize,
};
use crate::RootDatabase;
@ -102,6 +102,20 @@ pub(crate) fn signature_help(db: &RootDatabase, position: FilePosition) -> Optio
}
return signature_help_for_record_lit(&sema, record, token);
},
ast::RecordPat(record) => {
let cursor_outside = record.record_pat_field_list().and_then(|list| list.r_curly_token()).as_ref() == Some(&token);
if cursor_outside {
continue;
}
return signature_help_for_record_pat(&sema, record, token);
},
ast::TupleStructPat(tuple_pat) => {
let cursor_outside = tuple_pat.r_paren_token().as_ref() == Some(&token);
if cursor_outside {
continue;
}
return signature_help_for_tuple_struct_pat(&sema, tuple_pat, token);
},
_ => (),
}
}
@ -346,10 +360,111 @@ fn signature_help_for_record_lit(
record: ast::RecordExpr,
token: SyntaxToken,
) -> Option<SignatureHelp> {
let active_parameter = record
.record_expr_field_list()?
signature_help_for_record_(
sema,
record.record_expr_field_list()?.syntax().children_with_tokens(),
&record.path()?,
record
.record_expr_field_list()?
.fields()
.filter_map(|field| sema.resolve_record_field(&field))
.map(|(field, _, ty)| (field, ty)),
token,
)
}
fn signature_help_for_record_pat(
sema: &Semantics<'_, RootDatabase>,
record: ast::RecordPat,
token: SyntaxToken,
) -> Option<SignatureHelp> {
signature_help_for_record_(
sema,
record.record_pat_field_list()?.syntax().children_with_tokens(),
&record.path()?,
record
.record_pat_field_list()?
.fields()
.filter_map(|field| sema.resolve_record_pat_field(&field)),
token,
)
}
fn signature_help_for_tuple_struct_pat(
sema: &Semantics<'_, RootDatabase>,
pat: ast::TupleStructPat,
token: SyntaxToken,
) -> Option<SignatureHelp> {
let rest_pat = pat.fields().find(|it| matches!(it, ast::Pat::RestPat(_)));
let is_left_of_rest_pat =
rest_pat.map_or(true, |it| token.text_range().start() < it.syntax().text_range().end());
let mut res = SignatureHelp {
doc: None,
signature: String::new(),
parameters: vec![],
active_parameter: None,
};
let db = sema.db;
let path_res = sema.resolve_path(&pat.path()?)?;
let fields: Vec<_> = if let PathResolution::Def(ModuleDef::Variant(variant)) = path_res {
let en = variant.parent_enum(db);
res.doc = en.docs(db).map(|it| it.into());
format_to!(res.signature, "enum {}::{} (", en.name(db), variant.name(db));
variant.fields(db)
} else {
let adt = match path_res {
PathResolution::SelfType(imp) => imp.self_ty(db).as_adt()?,
PathResolution::Def(ModuleDef::Adt(adt)) => adt,
_ => return None,
};
match adt {
hir::Adt::Struct(it) => {
res.doc = it.docs(db).map(|it| it.into());
format_to!(res.signature, "struct {} (", it.name(db));
it.fields(db)
}
_ => return None,
}
};
let commas = pat
.syntax()
.children_with_tokens()
.filter_map(syntax::NodeOrToken::into_token)
.filter(|t| t.kind() == syntax::T![,]);
res.active_parameter = Some(if is_left_of_rest_pat {
commas.take_while(|t| t.text_range().start() <= token.text_range().start()).count()
} else {
let n_commas = commas
.collect::<Vec<_>>()
.into_iter()
.rev()
.take_while(|t| t.text_range().start() > token.text_range().start())
.count();
fields.len().saturating_sub(1).saturating_sub(n_commas)
});
let mut buf = String::new();
for ty in fields.into_iter().map(|it| it.ty(db)) {
format_to!(buf, "{}", ty.display_truncated(db, Some(20)));
res.push_call_param(&buf);
buf.clear();
}
res.signature.push_str(")");
Some(res)
}
fn signature_help_for_record_(
sema: &Semantics<'_, RootDatabase>,
field_list_children: SyntaxElementChildren,
path: &ast::Path,
fields2: impl Iterator<Item = (hir::Field, hir::Type)>,
token: SyntaxToken,
) -> Option<SignatureHelp> {
let active_parameter = field_list_children
.filter_map(syntax::NodeOrToken::into_token)
.filter(|t| t.kind() == syntax::T![,])
.take_while(|t| t.text_range().start() <= token.text_range().start())
@ -365,7 +480,7 @@ fn signature_help_for_record_lit(
let fields;
let db = sema.db;
let path_res = sema.resolve_path(&record.path()?)?;
let path_res = sema.resolve_path(path)?;
if let PathResolution::Def(ModuleDef::Variant(variant)) = path_res {
fields = variant.fields(db);
let en = variant.parent_enum(db);
@ -397,8 +512,7 @@ fn signature_help_for_record_lit(
let mut fields =
fields.into_iter().map(|field| (field.name(db), Some(field))).collect::<FxIndexMap<_, _>>();
let mut buf = String::new();
for field in record.record_expr_field_list()?.fields() {
let Some((field, _, ty)) = sema.resolve_record_field(&field) else { continue };
for (field, ty) in fields2 {
let name = field.name(db);
format_to!(buf, "{name}: {}", ty.display_truncated(db, Some(20)));
res.push_record_field(&buf);
@ -439,6 +553,7 @@ mod tests {
(database, FilePosition { file_id, offset })
}
#[track_caller]
fn check(ra_fixture: &str, expect: Expect) {
let fixture = format!(
r#"
@ -890,6 +1005,119 @@ fn main() {
);
}
#[test]
fn tuple_struct_pat() {
check(
r#"
/// A cool tuple struct
struct S(u32, i32);
fn main() {
let S(0, $0);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32)
--- ^^^
"#]],
);
}
#[test]
fn tuple_struct_pat_rest() {
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16);
fn main() {
let S(0, .., $0);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16)
--- --- --- ^^^
"#]],
);
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16, u8);
fn main() {
let S(0, .., $0, 0);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16, u8)
--- --- --- ^^^ --
"#]],
);
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16);
fn main() {
let S($0, .., 1);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16)
^^^ --- --- ---
"#]],
);
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16, u8);
fn main() {
let S(1, .., 1, $0, 2);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16, u8)
--- --- --- ^^^ --
"#]],
);
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16);
fn main() {
let S(1, $0.., 1);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16)
--- ^^^ --- ---
"#]],
);
check(
r#"
/// A cool tuple struct
struct S(u32, i32, f32, u16);
fn main() {
let S(1, ..$0, 1);
}
"#,
expect![[r#"
A cool tuple struct
------
struct S (u32, i32, f32, u16)
--- ^^^ --- ---
"#]],
);
}
#[test]
fn generic_struct() {
check(
@ -1550,6 +1778,29 @@ impl S {
);
}
#[test]
fn record_pat() {
check(
r#"
struct Strukt<T, U = ()> {
t: T,
u: U,
unit: (),
}
fn f() {
let Strukt {
u: 0,
$0
}
}
"#,
expect![[r#"
struct Strukt { u: i32, t: T, unit: () }
------ ^^^^ --------
"#]],
);
}
#[test]
fn test_enum_in_nested_method_in_lambda() {
check(