Skip to content

Commit b8e09b7

Browse files
committed
Inline lang items in coercion tests
1 parent 3b6979b commit b8e09b7

File tree

2 files changed

+65
-27
lines changed

2 files changed

+65
-27
lines changed

crates/ra_hir_ty/src/tests.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,22 +346,8 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
346346
}
347347
}
348348

349-
// Infer with some common definitions and impls.
350349
fn check_infer(ra_fixture: &str, expect: Expect) {
351-
let defs = r#"
352-
#[lang = "sized"]
353-
pub trait Sized {}
354-
#[lang = "unsize"]
355-
pub trait Unsize<T: ?Sized> {}
356-
#[lang = "coerce_unsized"]
357-
pub trait CoerceUnsized<T> {}
358-
359-
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
360-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
361-
"#;
362-
363-
// Append to the end to keep positions unchanged.
364-
let mut actual = infer(&format!("{}{}", ra_fixture, defs));
350+
let mut actual = infer(ra_fixture);
365351
actual.push('\n');
366352
expect.assert_eq(&actual);
367353
}

crates/ra_hir_ty/src/tests/coercion.rs

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn infer_block_expr_type_mismatch() {
2323
#[test]
2424
fn coerce_places() {
2525
check_infer(
26-
r"
26+
r#"
2727
struct S<T> { a: T }
2828
2929
fn f<T>(_: &[T]) -> T { loop {} }
@@ -45,7 +45,17 @@ fn coerce_places() {
4545
let f: [&[_]; 2] = [arr; 2];
4646
let g: (&[_], &[_]) = (arr, arr);
4747
}
48-
",
48+
49+
#[lang = "sized"]
50+
pub trait Sized {}
51+
#[lang = "unsize"]
52+
pub trait Unsize<T: ?Sized> {}
53+
#[lang = "coerce_unsized"]
54+
pub trait CoerceUnsized<T> {}
55+
56+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
57+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
58+
"#,
4959
expect![[r"
5060
30..31 '_': &[T]
5161
44..55 '{ loop {} }': T
@@ -121,7 +131,7 @@ fn infer_let_stmt_coerce() {
121131
#[test]
122132
fn infer_custom_coerce_unsized() {
123133
check_infer(
124-
r"
134+
r#"
125135
struct A<T: ?Sized>(*const T);
126136
struct B<T: ?Sized>(*const T);
127137
struct C<T: ?Sized> { inner: *const T }
@@ -138,7 +148,18 @@ fn infer_custom_coerce_unsized() {
138148
let e = foo2(b);
139149
let f = foo3(c);
140150
}
141-
",
151+
152+
153+
#[lang = "sized"]
154+
pub trait Sized {}
155+
#[lang = "unsize"]
156+
pub trait Unsize<T: ?Sized> {}
157+
#[lang = "coerce_unsized"]
158+
pub trait CoerceUnsized<T> {}
159+
160+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
161+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
162+
"#,
142163
expect![[r"
143164
257..258 'x': A<[T]>
144165
278..283 '{ x }': A<[T]>
@@ -172,7 +193,7 @@ fn infer_custom_coerce_unsized() {
172193
#[test]
173194
fn infer_if_coerce() {
174195
check_infer(
175-
r"
196+
r#"
176197
fn foo<T>(x: &[T]) -> &[T] { loop {} }
177198
fn test() {
178199
let x = if true {
@@ -181,7 +202,13 @@ fn infer_if_coerce() {
181202
&[1]
182203
};
183204
}
184-
",
205+
206+
207+
#[lang = "sized"]
208+
pub trait Sized {}
209+
#[lang = "unsize"]
210+
pub trait Unsize<T: ?Sized> {}
211+
"#,
185212
expect![[r"
186213
10..11 'x': &[T]
187214
27..38 '{ loop {} }': &[T]
@@ -208,7 +235,7 @@ fn infer_if_coerce() {
208235
#[test]
209236
fn infer_if_else_coerce() {
210237
check_infer(
211-
r"
238+
r#"
212239
fn foo<T>(x: &[T]) -> &[T] { loop {} }
213240
fn test() {
214241
let x = if true {
@@ -217,7 +244,17 @@ fn infer_if_else_coerce() {
217244
foo(&[1])
218245
};
219246
}
220-
",
247+
248+
#[lang = "sized"]
249+
pub trait Sized {}
250+
#[lang = "unsize"]
251+
pub trait Unsize<T: ?Sized> {}
252+
#[lang = "coerce_unsized"]
253+
pub trait CoerceUnsized<T> {}
254+
255+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
256+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
257+
"#,
221258
expect![[r"
222259
10..11 'x': &[T]
223260
27..38 '{ loop {} }': &[T]
@@ -244,7 +281,7 @@ fn infer_if_else_coerce() {
244281
#[test]
245282
fn infer_match_first_coerce() {
246283
check_infer(
247-
r"
284+
r#"
248285
fn foo<T>(x: &[T]) -> &[T] { loop {} }
249286
fn test(i: i32) {
250287
let x = match i {
@@ -253,7 +290,12 @@ fn infer_match_first_coerce() {
253290
_ => &[3],
254291
};
255292
}
256-
",
293+
294+
#[lang = "sized"]
295+
pub trait Sized {}
296+
#[lang = "unsize"]
297+
pub trait Unsize<T: ?Sized> {}
298+
"#,
257299
expect![[r"
258300
10..11 'x': &[T]
259301
27..38 '{ loop {} }': &[T]
@@ -287,7 +329,7 @@ fn infer_match_first_coerce() {
287329
#[test]
288330
fn infer_match_second_coerce() {
289331
check_infer(
290-
r"
332+
r#"
291333
fn foo<T>(x: &[T]) -> &[T] { loop {} }
292334
fn test(i: i32) {
293335
let x = match i {
@@ -296,7 +338,17 @@ fn infer_match_second_coerce() {
296338
_ => &[3],
297339
};
298340
}
299-
",
341+
342+
#[lang = "sized"]
343+
pub trait Sized {}
344+
#[lang = "unsize"]
345+
pub trait Unsize<T: ?Sized> {}
346+
#[lang = "coerce_unsized"]
347+
pub trait CoerceUnsized<T> {}
348+
349+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
350+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
351+
"#,
300352
expect![[r"
301353
10..11 'x': &[T]
302354
27..38 '{ loop {} }': &[T]

0 commit comments

Comments
 (0)