Skip to content

Commit 585e4d6

Browse files
committed
Do not explicit generics to generated expressions
1 parent 8fd7ae9 commit 585e4d6

File tree

3 files changed

+13
-75
lines changed

3 files changed

+13
-75
lines changed

src/tools/rust-analyzer/crates/hir/src/term_search/expr.rs

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use hir_ty::{
99
use itertools::Itertools;
1010

1111
use crate::{
12-
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Field, Function, GenericDef, Local,
13-
ModuleDef, SemanticsScope, Static, Struct, StructKind, Trait, Type, Variant,
12+
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Field, Function, Local, ModuleDef,
13+
SemanticsScope, Static, Struct, StructKind, Trait, Type, Variant,
1414
};
1515

1616
/// Helper function to get path to `ModuleDef`
@@ -35,43 +35,6 @@ fn mod_item_path_str(
3535
.ok_or(DisplaySourceCodeError::PathNotFound)
3636
}
3737

38-
/// Helper function to get path to `Type`
39-
fn type_path(
40-
sema_scope: &SemanticsScope<'_>,
41-
ty: &Type,
42-
cfg: ImportPathConfig,
43-
) -> Result<String, DisplaySourceCodeError> {
44-
let db = sema_scope.db;
45-
let m = sema_scope.module();
46-
47-
match ty.as_adt() {
48-
Some(adt) => {
49-
let ty_name = ty.display_source_code(db, m.id, true)?;
50-
51-
let mut path = mod_item_path(sema_scope, &ModuleDef::Adt(adt), cfg).unwrap();
52-
path.pop_segment();
53-
let path = path.display(db.upcast()).to_string();
54-
let res = match path.is_empty() {
55-
true => ty_name,
56-
false => format!("{path}::{ty_name}"),
57-
};
58-
Ok(res)
59-
}
60-
None => ty.display_source_code(db, m.id, true),
61-
}
62-
}
63-
64-
/// Helper function to filter out generic parameters that are default
65-
fn non_default_generics(db: &dyn HirDatabase, def: GenericDef, generics: &[Type]) -> Vec<Type> {
66-
def.type_or_const_params(db)
67-
.into_iter()
68-
.filter_map(|it| it.as_type_param(db))
69-
.zip(generics)
70-
.filter(|(tp, arg)| tp.default(db).as_ref() != Some(arg))
71-
.map(|(_, arg)| arg.clone())
72-
.collect()
73-
}
74-
7538
/// Type tree shows how can we get from set of types to some type.
7639
///
7740
/// Consider the following code as an example
@@ -208,20 +171,7 @@ impl Expr {
208171
None => Ok(format!("{target_str}.{func_name}({args})")),
209172
}
210173
}
211-
Expr::Variant { variant, generics, params } => {
212-
let generics = non_default_generics(db, variant.parent_enum(db).into(), generics);
213-
let generics_str = match generics.is_empty() {
214-
true => String::new(),
215-
false => {
216-
let generics = generics
217-
.iter()
218-
.map(|it| type_path(sema_scope, it, cfg))
219-
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
220-
.into_iter()
221-
.join(", ");
222-
format!("::<{generics}>")
223-
}
224-
};
174+
Expr::Variant { variant, params, .. } => {
225175
let inner = match variant.kind(db) {
226176
StructKind::Tuple => {
227177
let args = params
@@ -230,7 +180,7 @@ impl Expr {
230180
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
231181
.into_iter()
232182
.join(", ");
233-
format!("{generics_str}({args})")
183+
format!("({args})")
234184
}
235185
StructKind::Record => {
236186
let fields = variant.fields(db);
@@ -248,16 +198,15 @@ impl Expr {
248198
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
249199
.into_iter()
250200
.join(", ");
251-
format!("{generics_str}{{ {args} }}")
201+
format!("{{ {args} }}")
252202
}
253-
StructKind::Unit => generics_str,
203+
StructKind::Unit => String::new(),
254204
};
255205

256206
let prefix = mod_item_path_str(sema_scope, &ModuleDef::Variant(*variant))?;
257207
Ok(format!("{prefix}{inner}"))
258208
}
259-
Expr::Struct { strukt, generics, params } => {
260-
let generics = non_default_generics(db, (*strukt).into(), generics);
209+
Expr::Struct { strukt, params, .. } => {
261210
let inner = match strukt.kind(db) {
262211
StructKind::Tuple => {
263212
let args = params
@@ -286,18 +235,7 @@ impl Expr {
286235
.join(", ");
287236
format!(" {{ {args} }}")
288237
}
289-
StructKind::Unit => match generics.is_empty() {
290-
true => String::new(),
291-
false => {
292-
let generics = generics
293-
.iter()
294-
.map(|it| type_path(sema_scope, it, cfg))
295-
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
296-
.into_iter()
297-
.join(", ");
298-
format!("::<{generics}>")
299-
}
300-
},
238+
StructKind::Unit => String::new(),
301239
};
302240

303241
let prefix = mod_item_path_str(sema_scope, &ModuleDef::Adt(Adt::Struct(*strukt)))?;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn f() { let a = A { x: 1, y: true }; let b: i32 = a.x; }"#,
148148
term_search,
149149
r#"//- minicore: todo, unimplemented, option
150150
fn f() { let a: i32 = 1; let b: Option<i32> = todo$0!(); }"#,
151-
r#"fn f() { let a: i32 = 1; let b: Option<i32> = Some::<i32>(a); }"#,
151+
r#"fn f() { let a: i32 = 1; let b: Option<i32> = Some(a); }"#,
152152
)
153153
}
154154

@@ -160,7 +160,7 @@ fn f() { let a: i32 = 1; let b: Option<i32> = todo$0!(); }"#,
160160
enum Option<T> { None, Some(T) }
161161
fn f() { let a: i32 = 1; let b: Option<i32> = todo$0!(); }"#,
162162
r#"enum Option<T> { None, Some(T) }
163-
fn f() { let a: i32 = 1; let b: Option<i32> = Option::Some::<i32>(a); }"#,
163+
fn f() { let a: i32 = 1; let b: Option<i32> = Option::Some(a); }"#,
164164
)
165165
}
166166

@@ -172,7 +172,7 @@ fn f() { let a: i32 = 1; let b: Option<i32> = Option::Some::<i32>(a); }"#,
172172
enum Option<T> { None, Some(T) }
173173
fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = todo$0!(); }"#,
174174
r#"enum Option<T> { None, Some(T) }
175-
fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = Option::Some::<Option<i32>>(a); }"#,
175+
fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = Option::Some(a); }"#,
176176
)
177177
}
178178

@@ -184,7 +184,7 @@ fn f() { let a: Option<i32> = Option::None; let b: Option<Option<i32>> = Option:
184184
enum Foo<T = i32> { Foo(T) }
185185
fn f() { let a = 0; let b: Foo = todo$0!(); }"#,
186186
r#"enum Foo<T = i32> { Foo(T) }
187-
fn f() { let a = 0; let b: Foo = Foo::Foo::<i32>(a); }"#,
187+
fn f() { let a = 0; let b: Foo = Foo::Foo(a); }"#,
188188
);
189189

190190
check_assist(

src/tools/rust-analyzer/crates/ide-completion/src/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,7 @@ fn foo() {
26412641
expect![[r#"
26422642
lc foo [type+local]
26432643
ex foo [type]
2644-
ex Foo::B::<u32> [type]
2644+
ex Foo::B [type]
26452645
ev Foo::A(…) [type_could_unify]
26462646
ev Foo::B [type_could_unify]
26472647
en Foo [type_could_unify]

0 commit comments

Comments
 (0)