Skip to content

Commit b2a5127

Browse files
committed
Auto merge of rust-lang#17134 - Veykril:lt-err-display, r=Veykril
internal: Don't render unknown lifetimes when rendering generic args cc rust-lang/rust-analyzer#17098
2 parents ec65067 + a422ee3 commit b2a5127

File tree

15 files changed

+631
-586
lines changed

15 files changed

+631
-586
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+41-15
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,20 @@ impl HirDisplay for Ty {
797797
c.hir_fmt(f)?;
798798
write!(f, "]")?;
799799
}
800-
TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => {
801-
if matches!(self.kind(Interner), TyKind::Raw(..)) {
800+
kind @ (TyKind::Raw(m, t) | TyKind::Ref(m, _, t)) => {
801+
if let TyKind::Ref(_, l, _) = kind {
802+
f.write_char('&')?;
803+
if cfg!(test) {
804+
// rendering these unconditionally is probably too much (at least for inlay
805+
// hints) so we gate it to testing only for the time being
806+
l.hir_fmt(f)?;
807+
f.write_char(' ')?;
808+
}
809+
match m {
810+
Mutability::Not => (),
811+
Mutability::Mut => f.write_str("mut ")?,
812+
}
813+
} else {
802814
write!(
803815
f,
804816
"*{}",
@@ -807,15 +819,6 @@ impl HirDisplay for Ty {
807819
Mutability::Mut => "mut ",
808820
}
809821
)?;
810-
} else {
811-
write!(
812-
f,
813-
"&{}",
814-
match m {
815-
Mutability::Not => "",
816-
Mutability::Mut => "mut ",
817-
}
818-
)?;
819822
}
820823

821824
// FIXME: all this just to decide whether to use parentheses...
@@ -1330,7 +1333,18 @@ fn hir_fmt_generics(
13301333
}
13311334

13321335
let parameters_to_write = generic_args_sans_defaults(f, generic_def, parameters);
1333-
if !parameters_to_write.is_empty() {
1336+
1337+
// FIXME: Remote this
1338+
// most of our lifetimes will be errors as we lack elision and inference
1339+
// so don't render them for now
1340+
let only_err_lifetimes = !cfg!(test)
1341+
&& parameters_to_write.iter().all(|arg| {
1342+
matches!(
1343+
arg.data(Interner),
1344+
chalk_ir::GenericArgData::Lifetime(it) if *it.data(Interner) == LifetimeData::Error
1345+
)
1346+
});
1347+
if !parameters_to_write.is_empty() && !only_err_lifetimes {
13341348
write!(f, "<")?;
13351349
hir_fmt_generic_arguments(f, parameters_to_write)?;
13361350
write!(f, ">")?;
@@ -1403,6 +1417,18 @@ fn hir_fmt_generic_arguments(
14031417
None => (parameters, &[][..]),
14041418
};
14051419
for generic_arg in lifetimes.iter().chain(ty_or_const) {
1420+
// FIXME: Remove this
1421+
// most of our lifetimes will be errors as we lack elision and inference
1422+
// so don't render them for now
1423+
if !cfg!(test)
1424+
&& matches!(
1425+
generic_arg.lifetime(Interner),
1426+
Some(l) if ***l.interned() == LifetimeData::Error
1427+
)
1428+
{
1429+
continue;
1430+
}
1431+
14061432
if !first {
14071433
write!(f, ", ")?;
14081434
}
@@ -1728,9 +1754,9 @@ impl HirDisplay for LifetimeData {
17281754
LifetimeData::BoundVar(idx) => idx.hir_fmt(f),
17291755
LifetimeData::InferenceVar(_) => write!(f, "_"),
17301756
LifetimeData::Static => write!(f, "'static"),
1731-
LifetimeData::Error => write!(f, "'{{error}}"),
1732-
LifetimeData::Erased => Ok(()),
1733-
LifetimeData::Phantom(_, _) => Ok(()),
1757+
LifetimeData::Error => write!(f, "'?"),
1758+
LifetimeData::Erased => write!(f, "'<erased>"),
1759+
LifetimeData::Phantom(void, _) => match *void {},
17341760
}
17351761
}
17361762
}

src/tools/rust-analyzer/crates/hir-ty/src/tests/coercion.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn test() {
186186
let x = match 1 {
187187
1 => t as *mut i32,
188188
2 => t as &i32,
189-
//^^^^^^^^^ expected *mut i32, got &i32
189+
//^^^^^^^^^ expected *mut i32, got &'? i32
190190
_ => t as *const i32,
191191
// ^^^^^^^^^^^^^^^ adjustments: Pointer(MutToConstPointer)
192192
@@ -307,7 +307,7 @@ fn test() {
307307
let foo = Foo;
308308
//^^^ type: Foo<{unknown}>
309309
let _: &u32 = &Foo;
310-
//^^^^ expected &u32, got &Foo<{unknown}>
310+
//^^^^ expected &'? u32, got &'? Foo<{unknown}>
311311
}",
312312
);
313313
}
@@ -544,9 +544,9 @@ struct Bar<T>(Foo<T>);
544544
545545
fn test() {
546546
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
547-
//^^^^^^^^^^^^^^^^^^^^^ expected &Foo<[usize]>, got &Foo<[i32; 3]>
547+
//^^^^^^^^^^^^^^^^^^^^^ expected &'? Foo<[usize]>, got &'? Foo<[i32; 3]>
548548
let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
549-
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &Bar<[usize]>, got &Bar<[i32; 3]>
549+
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &'? Bar<[usize]>, got &'? Bar<[i32; 3]>
550550
}
551551
"#,
552552
);
@@ -562,7 +562,7 @@ trait Foo {}
562562
fn test(f: impl Foo, g: &(impl Foo + ?Sized)) {
563563
let _: &dyn Foo = &f;
564564
let _: &dyn Foo = g;
565-
//^ expected &dyn Foo, got &impl Foo + ?Sized
565+
//^ expected &'? dyn Foo, got &'? impl Foo + ?Sized
566566
}
567567
"#,
568568
);
@@ -828,11 +828,11 @@ struct V<T> { t: T }
828828
fn main() {
829829
let a: V<&dyn Tr>;
830830
(a,) = V { t: &S };
831-
//^^^^expected V<&S>, got (V<&dyn Tr>,)
831+
//^^^^expected V<&'? S>, got (V<&'? dyn Tr>,)
832832
833833
let mut a: V<&dyn Tr> = V { t: &S };
834834
(a,) = V { t: &S };
835-
//^^^^expected V<&S>, got (V<&dyn Tr>,)
835+
//^^^^expected V<&'? S>, got (V<&'? dyn Tr>,)
836836
}
837837
"#,
838838
);

src/tools/rust-analyzer/crates/hir-ty/src/tests/diagnostics.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn function_return_type_mismatch_1() {
88
r#"
99
fn test() -> &'static str {
1010
5
11-
//^ expected &str, got i32
11+
//^ expected &'static str, got i32
1212
}
1313
"#,
1414
);
@@ -21,7 +21,7 @@ fn function_return_type_mismatch_2() {
2121
fn test(x: bool) -> &'static str {
2222
if x {
2323
return 1;
24-
//^ expected &str, got i32
24+
//^ expected &'static str, got i32
2525
}
2626
"ok"
2727
}
@@ -38,7 +38,7 @@ fn test(x: bool) -> &'static str {
3838
return "ok";
3939
}
4040
1
41-
//^ expected &str, got i32
41+
//^ expected &'static str, got i32
4242
}
4343
"#,
4444
);
@@ -53,7 +53,7 @@ fn test(x: bool) -> &'static str {
5353
"ok"
5454
} else {
5555
1
56-
//^ expected &str, got i32
56+
//^ expected &'static str, got i32
5757
}
5858
}
5959
"#,
@@ -67,7 +67,7 @@ fn function_return_type_mismatch_5() {
6767
fn test(x: bool) -> &'static str {
6868
if x {
6969
1
70-
//^ expected &str, got i32
70+
//^ expected &'static str, got i32
7171
} else {
7272
"ok"
7373
}
@@ -83,10 +83,10 @@ fn non_unit_block_expr_stmt_no_semi() {
8383
fn test(x: bool) {
8484
if x {
8585
"notok"
86-
//^^^^^^^ expected (), got &str
86+
//^^^^^^^ expected (), got &'static str
8787
} else {
8888
"ok"
89-
//^^^^ expected (), got &str
89+
//^^^^ expected (), got &'static str
9090
}
9191
match x { true => true, false => 0 }
9292
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), got bool

src/tools/rust-analyzer/crates/hir-ty/src/tests/display_source_code.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ trait B: A {}
6767
6868
fn test(
6969
_: &(dyn A<Assoc = ()> + Send),
70-
//^ &(dyn A<Assoc = ()> + Send)
70+
//^ &'_ (dyn A<Assoc = ()> + Send)
7171
_: &(dyn Send + A<Assoc = ()>),
72-
//^ &(dyn A<Assoc = ()> + Send)
72+
//^ &'_ (dyn A<Assoc = ()> + Send)
7373
_: &dyn B<Assoc = ()>,
74-
//^ &(dyn B<Assoc = ()>)
74+
//^ &'_ (dyn B<Assoc = ()>)
7575
) {}
7676
"#,
7777
);
@@ -85,7 +85,7 @@ fn render_dyn_for_ty() {
8585
trait Foo<'a> {}
8686
8787
fn foo(foo: &dyn for<'a> Foo<'a>) {}
88-
// ^^^ &dyn Foo<'_>
88+
// ^^^ &'_ dyn Foo<'_>
8989
"#,
9090
);
9191
}
@@ -111,11 +111,11 @@ fn test(
111111
b;
112112
//^ impl Foo
113113
c;
114-
//^ &impl Foo + ?Sized
114+
//^ &'_ impl Foo + ?Sized
115115
d;
116116
//^ S<impl Foo>
117117
ref_any;
118-
//^^^^^^^ &impl ?Sized
118+
//^^^^^^^ &'_ impl ?Sized
119119
empty;
120120
} //^^^^^ impl Sized
121121
"#,
@@ -192,7 +192,7 @@ fn test(
192192
b;
193193
//^ fn(impl Foo) -> impl Foo
194194
c;
195-
} //^ fn(&impl Foo + ?Sized) -> &impl Foo + ?Sized
195+
} //^ fn(&'_ impl Foo + ?Sized) -> &'_ impl Foo + ?Sized
196196
"#,
197197
);
198198
}

src/tools/rust-analyzer/crates/hir-ty/src/tests/macros.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ fn expr_macro_def_expanded_in_various_places() {
200200
100..119 'for _ ...!() {}': IntoIterator::IntoIter<isize>
201201
100..119 'for _ ...!() {}': !
202202
100..119 'for _ ...!() {}': IntoIterator::IntoIter<isize>
203-
100..119 'for _ ...!() {}': &mut IntoIterator::IntoIter<isize>
204-
100..119 'for _ ...!() {}': fn next<IntoIterator::IntoIter<isize>>(&mut IntoIterator::IntoIter<isize>) -> Option<<IntoIterator::IntoIter<isize> as Iterator>::Item>
203+
100..119 'for _ ...!() {}': &'? mut IntoIterator::IntoIter<isize>
204+
100..119 'for _ ...!() {}': fn next<IntoIterator::IntoIter<isize>>(&'? mut IntoIterator::IntoIter<isize>) -> Option<<IntoIterator::IntoIter<isize> as Iterator>::Item>
205205
100..119 'for _ ...!() {}': Option<IntoIterator::Item<isize>>
206206
100..119 'for _ ...!() {}': ()
207207
100..119 'for _ ...!() {}': ()
@@ -221,7 +221,7 @@ fn expr_macro_def_expanded_in_various_places() {
221221
281..303 'Spam {...m!() }': {unknown}
222222
309..325 'spam!(...am!()]': {unknown}
223223
350..366 'spam!(... usize': usize
224-
372..380 '&spam!()': &isize
224+
372..380 '&spam!()': &'? isize
225225
386..394 '-spam!()': isize
226226
400..416 'spam!(...pam!()': {unknown}
227227
422..439 'spam!(...pam!()': isize
@@ -293,8 +293,8 @@ fn expr_macro_rules_expanded_in_various_places() {
293293
114..133 'for _ ...!() {}': IntoIterator::IntoIter<isize>
294294
114..133 'for _ ...!() {}': !
295295
114..133 'for _ ...!() {}': IntoIterator::IntoIter<isize>
296-
114..133 'for _ ...!() {}': &mut IntoIterator::IntoIter<isize>
297-
114..133 'for _ ...!() {}': fn next<IntoIterator::IntoIter<isize>>(&mut IntoIterator::IntoIter<isize>) -> Option<<IntoIterator::IntoIter<isize> as Iterator>::Item>
296+
114..133 'for _ ...!() {}': &'? mut IntoIterator::IntoIter<isize>
297+
114..133 'for _ ...!() {}': fn next<IntoIterator::IntoIter<isize>>(&'? mut IntoIterator::IntoIter<isize>) -> Option<<IntoIterator::IntoIter<isize> as Iterator>::Item>
298298
114..133 'for _ ...!() {}': Option<IntoIterator::Item<isize>>
299299
114..133 'for _ ...!() {}': ()
300300
114..133 'for _ ...!() {}': ()
@@ -314,7 +314,7 @@ fn expr_macro_rules_expanded_in_various_places() {
314314
295..317 'Spam {...m!() }': {unknown}
315315
323..339 'spam!(...am!()]': {unknown}
316316
364..380 'spam!(... usize': usize
317-
386..394 '&spam!()': &isize
317+
386..394 '&spam!()': &'? isize
318318
400..408 '-spam!()': isize
319319
414..430 'spam!(...pam!()': {unknown}
320320
436..453 'spam!(...pam!()': isize
@@ -539,7 +539,7 @@ fn test() {
539539
let msg = foo::Message(foo::MessageRef);
540540
let r = msg.deref();
541541
r;
542-
//^ &MessageRef
542+
//^ &'? MessageRef
543543
}
544544
545545
//- /lib.rs crate:foo
@@ -703,9 +703,9 @@ fn infer_builtin_macros_file() {
703703
}
704704
"#,
705705
expect![[r#"
706-
!0..2 '""': &str
706+
!0..2 '""': &'static str
707707
63..87 '{ ...!(); }': ()
708-
73..74 'x': &str
708+
73..74 'x': &'static str
709709
"#]],
710710
);
711711
}
@@ -741,9 +741,9 @@ fn infer_builtin_macros_concat() {
741741
}
742742
"#,
743743
expect![[r#"
744-
!0..13 '"helloworld!"': &str
744+
!0..13 '"helloworld!"': &'static str
745745
65..121 '{ ...")); }': ()
746-
75..76 'x': &str
746+
75..76 'x': &'static str
747747
"#]],
748748
);
749749
}
@@ -820,7 +820,7 @@ macro_rules! include_str {() => {}}
820820
fn main() {
821821
let a = include_str!("foo.rs");
822822
a;
823-
} //^ &str
823+
} //^ &'static str
824824
825825
//- /foo.rs
826826
hello
@@ -847,7 +847,7 @@ macro_rules! m {
847847
fn main() {
848848
let a = include_str!(m!(".rs"));
849849
a;
850-
} //^ &str
850+
} //^ &'static str
851851
852852
//- /foo.rs
853853
hello
@@ -960,9 +960,9 @@ fn infer_builtin_macros_concat_with_lazy() {
960960
}
961961
"#,
962962
expect![[r#"
963-
!0..13 '"helloworld!"': &str
963+
!0..13 '"helloworld!"': &'static str
964964
103..160 '{ ...")); }': ()
965-
113..114 'x': &str
965+
113..114 'x': &'static str
966966
"#]],
967967
);
968968
}
@@ -977,7 +977,7 @@ fn infer_builtin_macros_env() {
977977
978978
fn main() {
979979
let x = env!("foo");
980-
//^ &str
980+
//^ &'static str
981981
}
982982
"#,
983983
);
@@ -991,7 +991,7 @@ fn infer_builtin_macros_option_env() {
991991
//- /main.rs env:foo=bar
992992
fn main() {
993993
let x = option_env!("foo");
994-
//^ Option<&str>
994+
//^ Option<&'static str>
995995
}
996996
"#,
997997
);

0 commit comments

Comments
 (0)