aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs303
-rw-r--r--crates/ra_assists/src/lib.rs2
-rw-r--r--crates/ra_assists/src/tests/generated.rs25
-rw-r--r--crates/ra_hir_ty/Cargo.toml4
-rw-r--r--crates/ra_hir_ty/src/infer/coerce.rs4
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs16
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs50
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs2
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs54
9 files changed, 444 insertions, 16 deletions
diff --git a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs b/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
new file mode 100644
index 000000000..999aec421
--- /dev/null
+++ b/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
@@ -0,0 +1,303 @@
1use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists};
2use ast::{NameOwner, ParamList, TypeAscriptionOwner, TypeParamList, TypeRef};
3use ra_syntax::{ast, ast::TypeParamsOwner, AstNode, SyntaxKind, TextRange, TextSize};
4use rustc_hash::FxHashSet;
5
6static ASSIST_NAME: &str = "change_lifetime_anon_to_named";
7static ASSIST_LABEL: &str = "Give anonymous lifetime a name";
8
9// Assist: change_lifetime_anon_to_named
10//
11// Change an anonymous lifetime to a named lifetime.
12//
13// ```
14// impl Cursor<'_<|>> {
15// fn node(self) -> &SyntaxNode {
16// match self {
17// Cursor::Replace(node) | Cursor::Before(node) => node,
18// }
19// }
20// }
21// ```
22// ->
23// ```
24// impl<'a> Cursor<'a> {
25// fn node(self) -> &SyntaxNode {
26// match self {
27// Cursor::Replace(node) | Cursor::Before(node) => node,
28// }
29// }
30// }
31// ```
32// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
33// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
34pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
35 let lifetime_token = ctx
36 .find_token_at_offset(SyntaxKind::LIFETIME)
37 .filter(|lifetime| lifetime.text() == "'_")?;
38 if let Some(fn_def) = lifetime_token.ancestors().find_map(ast::FnDef::cast) {
39 generate_fn_def_assist(acc, &fn_def, lifetime_token.text_range())
40 } else if let Some(impl_def) = lifetime_token.ancestors().find_map(ast::ImplDef::cast) {
41 // only allow naming the last anonymous lifetime
42 lifetime_token.next_token().filter(|tok| tok.kind() == SyntaxKind::R_ANGLE)?;
43 generate_impl_def_assist(acc, &impl_def, lifetime_token.text_range())
44 } else {
45 None
46 }
47}
48
49/// Generate the assist for the fn def case
50fn generate_fn_def_assist(
51 acc: &mut Assists,
52 fn_def: &ast::FnDef,
53 lifetime_loc: TextRange,
54) -> Option<()> {
55 let param_list: ParamList = fn_def.param_list()?;
56 let new_lifetime_param = generate_unique_lifetime_param_name(&fn_def.type_param_list())?;
57 let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end();
58 let self_param =
59 // use the self if it's a reference and has no explicit lifetime
60 param_list.self_param().filter(|p| p.lifetime_token().is_none() && p.amp_token().is_some());
61 // compute the location which implicitly has the same lifetime as the anonymous lifetime
62 let loc_needing_lifetime = if let Some(self_param) = self_param {
63 // if we have a self reference, use that
64 Some(self_param.self_token()?.text_range().start())
65 } else {
66 // otherwise, if there's a single reference parameter without a named liftime, use that
67 let fn_params_without_lifetime: Vec<_> = param_list
68 .params()
69 .filter_map(|param| match param.ascribed_type() {
70 Some(TypeRef::ReferenceType(ascribed_type))
71 if ascribed_type.lifetime_token() == None =>
72 {
73 Some(ascribed_type.amp_token()?.text_range().end())
74 }
75 _ => None,
76 })
77 .collect();
78 match fn_params_without_lifetime.len() {
79 1 => Some(fn_params_without_lifetime.into_iter().nth(0)?),
80 0 => None,
81 // multiple unnnamed is invalid. assist is not applicable
82 _ => return None,
83 }
84 };
85 acc.add(AssistId(ASSIST_NAME), ASSIST_LABEL, lifetime_loc, |builder| {
86 add_lifetime_param(fn_def, builder, end_of_fn_ident, new_lifetime_param);
87 builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
88 loc_needing_lifetime.map(|loc| builder.insert(loc, format!("'{} ", new_lifetime_param)));
89 })
90}
91
92/// Generate the assist for the impl def case
93fn generate_impl_def_assist(
94 acc: &mut Assists,
95 impl_def: &ast::ImplDef,
96 lifetime_loc: TextRange,
97) -> Option<()> {
98 let new_lifetime_param = generate_unique_lifetime_param_name(&impl_def.type_param_list())?;
99 let end_of_impl_kw = impl_def.impl_token()?.text_range().end();
100 acc.add(AssistId(ASSIST_NAME), ASSIST_LABEL, lifetime_loc, |builder| {
101 add_lifetime_param(impl_def, builder, end_of_impl_kw, new_lifetime_param);
102 builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
103 })
104}
105
106/// Given a type parameter list, generate a unique lifetime parameter name
107/// which is not in the list
108fn generate_unique_lifetime_param_name(
109 existing_type_param_list: &Option<TypeParamList>,
110) -> Option<char> {
111 match existing_type_param_list {
112 Some(type_params) => {
113 let used_lifetime_params: FxHashSet<_> = type_params
114 .lifetime_params()
115 .map(|p| p.syntax().text().to_string()[1..].to_owned())
116 .collect();
117 (b'a'..=b'z').map(char::from).find(|c| !used_lifetime_params.contains(&c.to_string()))
118 }
119 None => Some('a'),
120 }
121}
122
123/// Add the lifetime param to `builder`. If there are type parameters in `type_params_owner`, add it to the end. Otherwise
124/// add new type params brackets with the lifetime parameter at `new_type_params_loc`.
125fn add_lifetime_param<TypeParamsOwner: ast::TypeParamsOwner>(
126 type_params_owner: &TypeParamsOwner,
127 builder: &mut AssistBuilder,
128 new_type_params_loc: TextSize,
129 new_lifetime_param: char,
130) {
131 match type_params_owner.type_param_list() {
132 // add the new lifetime parameter to an existing type param list
133 Some(type_params) => {
134 builder.insert(
135 (u32::from(type_params.syntax().text_range().end()) - 1).into(),
136 format!(", '{}", new_lifetime_param),
137 );
138 }
139 // create a new type param list containing only the new lifetime parameter
140 None => {
141 builder.insert(new_type_params_loc, format!("<'{}>", new_lifetime_param));
142 }
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149 use crate::tests::{check_assist, check_assist_not_applicable};
150
151 #[test]
152 fn test_example_case() {
153 check_assist(
154 change_lifetime_anon_to_named,
155 r#"impl Cursor<'_<|>> {
156 fn node(self) -> &SyntaxNode {
157 match self {
158 Cursor::Replace(node) | Cursor::Before(node) => node,
159 }
160 }
161 }"#,
162 r#"impl<'a> Cursor<'a> {
163 fn node(self) -> &SyntaxNode {
164 match self {
165 Cursor::Replace(node) | Cursor::Before(node) => node,
166 }
167 }
168 }"#,
169 );
170 }
171
172 #[test]
173 fn test_example_case_simplified() {
174 check_assist(
175 change_lifetime_anon_to_named,
176 r#"impl Cursor<'_<|>> {"#,
177 r#"impl<'a> Cursor<'a> {"#,
178 );
179 }
180
181 #[test]
182 fn test_example_case_cursor_after_tick() {
183 check_assist(
184 change_lifetime_anon_to_named,
185 r#"impl Cursor<'<|>_> {"#,
186 r#"impl<'a> Cursor<'a> {"#,
187 );
188 }
189
190 #[test]
191 fn test_example_case_cursor_before_tick() {
192 check_assist(
193 change_lifetime_anon_to_named,
194 r#"impl Cursor<<|>'_> {"#,
195 r#"impl<'a> Cursor<'a> {"#,
196 );
197 }
198
199 #[test]
200 fn test_not_applicable_cursor_position() {
201 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'_><|> {"#);
202 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<|><'_> {"#);
203 }
204
205 #[test]
206 fn test_not_applicable_lifetime_already_name() {
207 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'a<|>> {"#);
208 check_assist_not_applicable(
209 change_lifetime_anon_to_named,
210 r#"fn my_fun<'a>() -> X<'a<|>>"#,
211 );
212 }
213
214 #[test]
215 fn test_with_type_parameter() {
216 check_assist(
217 change_lifetime_anon_to_named,
218 r#"impl<T> Cursor<T, '_<|>>"#,
219 r#"impl<T, 'a> Cursor<T, 'a>"#,
220 );
221 }
222
223 #[test]
224 fn test_with_existing_lifetime_name_conflict() {
225 check_assist(
226 change_lifetime_anon_to_named,
227 r#"impl<'a, 'b> Cursor<'a, 'b, '_<|>>"#,
228 r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
229 );
230 }
231
232 #[test]
233 fn test_function_return_value_anon_lifetime_param() {
234 check_assist(
235 change_lifetime_anon_to_named,
236 r#"fn my_fun() -> X<'_<|>>"#,
237 r#"fn my_fun<'a>() -> X<'a>"#,
238 );
239 }
240
241 #[test]
242 fn test_function_return_value_anon_reference_lifetime() {
243 check_assist(
244 change_lifetime_anon_to_named,
245 r#"fn my_fun() -> &'_<|> X"#,
246 r#"fn my_fun<'a>() -> &'a X"#,
247 );
248 }
249
250 #[test]
251 fn test_function_param_anon_lifetime() {
252 check_assist(
253 change_lifetime_anon_to_named,
254 r#"fn my_fun(x: X<'_<|>>)"#,
255 r#"fn my_fun<'a>(x: X<'a>)"#,
256 );
257 }
258
259 #[test]
260 fn test_function_add_lifetime_to_params() {
261 check_assist(
262 change_lifetime_anon_to_named,
263 r#"fn my_fun(f: &Foo) -> X<'_<|>>"#,
264 r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
265 );
266 }
267
268 #[test]
269 fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
270 check_assist(
271 change_lifetime_anon_to_named,
272 r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
273 r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
274 );
275 }
276
277 #[test]
278 fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
279 // this is not permitted under lifetime elision rules
280 check_assist_not_applicable(
281 change_lifetime_anon_to_named,
282 r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_<|>>"#,
283 );
284 }
285
286 #[test]
287 fn test_function_add_lifetime_to_self_ref_param() {
288 check_assist(
289 change_lifetime_anon_to_named,
290 r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
291 r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
292 );
293 }
294
295 #[test]
296 fn test_function_add_lifetime_to_param_with_non_ref_self() {
297 check_assist(
298 change_lifetime_anon_to_named,
299 r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
300 r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
301 );
302 }
303}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 464bc03dd..3f8f7ffbf 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -112,6 +112,7 @@ mod handlers {
112 mod add_turbo_fish; 112 mod add_turbo_fish;
113 mod apply_demorgan; 113 mod apply_demorgan;
114 mod auto_import; 114 mod auto_import;
115 mod change_lifetime_anon_to_named;
115 mod change_return_type_to_result; 116 mod change_return_type_to_result;
116 mod change_visibility; 117 mod change_visibility;
117 mod early_return; 118 mod early_return;
@@ -151,6 +152,7 @@ mod handlers {
151 add_turbo_fish::add_turbo_fish, 152 add_turbo_fish::add_turbo_fish,
152 apply_demorgan::apply_demorgan, 153 apply_demorgan::apply_demorgan,
153 auto_import::auto_import, 154 auto_import::auto_import,
155 change_lifetime_anon_to_named::change_lifetime_anon_to_named,
154 change_return_type_to_result::change_return_type_to_result, 156 change_return_type_to_result::change_return_type_to_result,
155 change_visibility::change_visibility, 157 change_visibility::change_visibility,
156 early_return::convert_to_guarded_return, 158 early_return::convert_to_guarded_return,
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs
index 250e56a69..abffbf97c 100644
--- a/crates/ra_assists/src/tests/generated.rs
+++ b/crates/ra_assists/src/tests/generated.rs
@@ -269,6 +269,31 @@ pub mod std { pub mod collections { pub struct HashMap { } } }
269} 269}
270 270
271#[test] 271#[test]
272fn doctest_change_lifetime_anon_to_named() {
273 check_doc_test(
274 "change_lifetime_anon_to_named",
275 r#####"
276impl Cursor<'_<|>> {
277 fn node(self) -> &SyntaxNode {
278 match self {
279 Cursor::Replace(node) | Cursor::Before(node) => node,
280 }
281 }
282}
283"#####,
284 r#####"
285impl<'a> Cursor<'a> {
286 fn node(self) -> &SyntaxNode {
287 match self {
288 Cursor::Replace(node) | Cursor::Before(node) => node,
289 }
290 }
291}
292"#####,
293 )
294}
295
296#[test]
272fn doctest_change_return_type_to_result() { 297fn doctest_change_return_type_to_result() {
273 check_doc_test( 298 check_doc_test(
274 "change_return_type_to_result", 299 "change_return_type_to_result",
diff --git a/crates/ra_hir_ty/Cargo.toml b/crates/ra_hir_ty/Cargo.toml
index 6afed58a1..4b8dcdc07 100644
--- a/crates/ra_hir_ty/Cargo.toml
+++ b/crates/ra_hir_ty/Cargo.toml
@@ -27,8 +27,8 @@ test_utils = { path = "../test_utils" }
27 27
28scoped-tls = "1" 28scoped-tls = "1"
29 29
30chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "5a3b871ca17529ab5aa5787594fabad1634936cb" } 30chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "329b7f3fdd2431ed6f6778cde53f22374c7d094c" }
31chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "5a3b871ca17529ab5aa5787594fabad1634936cb" } 31chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "329b7f3fdd2431ed6f6778cde53f22374c7d094c" }
32 32
33[dev-dependencies] 33[dev-dependencies]
34insta = "0.16.0" 34insta = "0.16.0"
diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs
index 2ee9adb16..32c7c57cd 100644
--- a/crates/ra_hir_ty/src/infer/coerce.rs
+++ b/crates/ra_hir_ty/src/infer/coerce.rs
@@ -45,9 +45,7 @@ impl<'a> InferenceContext<'a> {
45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2) 45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2)
46 } else { 46 } else {
47 mark::hit!(coerce_merge_fail_fallback); 47 mark::hit!(coerce_merge_fail_fallback);
48 // For incompatible types, we use the latter one as result 48 ty1.clone()
49 // to be better recovery for `if` without `else`.
50 ty2.clone()
51 } 49 }
52 } 50 }
53 } 51 }
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index 54bab3476..78084cb57 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -140,13 +140,13 @@ impl<'a> InferenceContext<'a> {
140 140
141 let mut sig_tys = Vec::new(); 141 let mut sig_tys = Vec::new();
142 142
143 for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) { 143 // collect explicitly written argument types
144 let expected = if let Some(type_ref) = arg_type { 144 for arg_type in arg_types.iter() {
145 let arg_ty = if let Some(type_ref) = arg_type {
145 self.make_ty(type_ref) 146 self.make_ty(type_ref)
146 } else { 147 } else {
147 Ty::Unknown 148 self.table.new_type_var()
148 }; 149 };
149 let arg_ty = self.infer_pat(*arg_pat, &expected, BindingMode::default());
150 sig_tys.push(arg_ty); 150 sig_tys.push(arg_ty);
151 } 151 }
152 152
@@ -158,7 +158,7 @@ impl<'a> InferenceContext<'a> {
158 sig_tys.push(ret_ty.clone()); 158 sig_tys.push(ret_ty.clone());
159 let sig_ty = Ty::apply( 159 let sig_ty = Ty::apply(
160 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, 160 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 },
161 Substs(sig_tys.into()), 161 Substs(sig_tys.clone().into()),
162 ); 162 );
163 let closure_ty = 163 let closure_ty =
164 Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); 164 Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty);
@@ -168,6 +168,12 @@ impl<'a> InferenceContext<'a> {
168 // infer the body. 168 // infer the body.
169 self.coerce(&closure_ty, &expected.ty); 169 self.coerce(&closure_ty, &expected.ty);
170 170
171 // Now go through the argument patterns
172 for (arg_pat, arg_ty) in args.iter().zip(sig_tys) {
173 let resolved = self.resolve_ty_as_possible(arg_ty);
174 self.infer_pat(*arg_pat, &resolved, BindingMode::default());
175 }
176
171 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); 177 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
172 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone()); 178 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone());
173 179
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
index 0c5f972a2..fe62587c0 100644
--- a/crates/ra_hir_ty/src/tests/patterns.rs
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -520,3 +520,53 @@ fn main() {
520 105..107 '()': () 520 105..107 '()': ()
521 ") 521 ")
522} 522}
523
524#[test]
525fn match_ergonomics_in_closure_params() {
526 assert_snapshot!(
527 infer(r#"
528#[lang = "fn_once"]
529trait FnOnce<Args> {
530 type Output;
531}
532
533fn foo<T, U, F: FnOnce(T) -> U>(t: T, f: F) -> U { loop {} }
534
535fn test() {
536 foo(&(1, "a"), |&(x, y)| x); // normal, no match ergonomics
537 foo(&(1, "a"), |(x, y)| x);
538}
539"#),
540 @r###"
541 94..95 't': T
542 100..101 'f': F
543 111..122 '{ loop {} }': U
544 113..120 'loop {}': !
545 118..120 '{}': ()
546 134..233 '{ ... x); }': ()
547 140..143 'foo': fn foo<&(i32, &str), i32, |&(i32, &str)| -> i32>(&(i32, &str), |&(i32, &str)| -> i32) -> i32
548 140..167 'foo(&(...y)| x)': i32
549 144..153 '&(1, "a")': &(i32, &str)
550 145..153 '(1, "a")': (i32, &str)
551 146..147 '1': i32
552 149..152 '"a"': &str
553 155..166 '|&(x, y)| x': |&(i32, &str)| -> i32
554 156..163 '&(x, y)': &(i32, &str)
555 157..163 '(x, y)': (i32, &str)
556 158..159 'x': i32
557 161..162 'y': &str
558 165..166 'x': i32
559 204..207 'foo': fn foo<&(i32, &str), &i32, |&(i32, &str)| -> &i32>(&(i32, &str), |&(i32, &str)| -> &i32) -> &i32
560 204..230 'foo(&(...y)| x)': &i32
561 208..217 '&(1, "a")': &(i32, &str)
562 209..217 '(1, "a")': (i32, &str)
563 210..211 '1': i32
564 213..216 '"a"': &str
565 219..229 '|(x, y)| x': |&(i32, &str)| -> &i32
566 220..226 '(x, y)': (i32, &str)
567 221..222 'x': &i32
568 224..225 'y': &&str
569 228..229 'x': &i32
570 "###
571 );
572}
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index f1db34160..839491b9e 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -957,7 +957,7 @@ fn main(foo: Foo) {
957 51..107 'if tru... }': () 957 51..107 'if tru... }': ()
958 54..58 'true': bool 958 54..58 'true': bool
959 59..67 '{ }': () 959 59..67 '{ }': ()
960 73..107 'if fal... }': () 960 73..107 'if fal... }': i32
961 76..81 'false': bool 961 76..81 'false': bool
962 82..107 '{ ... }': i32 962 82..107 '{ ... }': i32
963 92..95 'foo': Foo 963 92..95 'foo': Foo
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
index 0419bc751..e8778d419 100644
--- a/crates/ra_hir_ty/src/tests/traits.rs
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -2665,7 +2665,6 @@ fn test() {
2665 Enum::Variant.test(); 2665 Enum::Variant.test();
2666} 2666}
2667"#, true), 2667"#, true),
2668 // wrong result, because the built-in Copy impl for fn defs doesn't exist in Chalk yet
2669 @r###" 2668 @r###"
2670 42..44 '{}': () 2669 42..44 '{}': ()
2671 61..62 'T': {unknown} 2670 61..62 'T': {unknown}
@@ -2674,13 +2673,13 @@ fn test() {
2674 146..150 'self': &Self 2673 146..150 'self': &Self
2675 202..282 '{ ...t(); }': () 2674 202..282 '{ ...t(); }': ()
2676 208..211 'foo': fn foo() 2675 208..211 'foo': fn foo()
2677 208..218 'foo.test()': {unknown} 2676 208..218 'foo.test()': bool
2678 224..227 'bar': fn bar<{unknown}>({unknown}) -> {unknown} 2677 224..227 'bar': fn bar<{unknown}>({unknown}) -> {unknown}
2679 224..234 'bar.test()': {unknown} 2678 224..234 'bar.test()': bool
2680 240..246 'Struct': Struct(usize) -> Struct 2679 240..246 'Struct': Struct(usize) -> Struct
2681 240..253 'Struct.test()': {unknown} 2680 240..253 'Struct.test()': bool
2682 259..272 'Enum::Variant': Variant(usize) -> Enum 2681 259..272 'Enum::Variant': Variant(usize) -> Enum
2683 259..279 'Enum::...test()': {unknown} 2682 259..279 'Enum::...test()': bool
2684 "### 2683 "###
2685 ); 2684 );
2686} 2685}
@@ -2754,3 +2753,48 @@ fn test() {
2754 "### 2753 "###
2755 ); 2754 );
2756} 2755}
2756
2757#[test]
2758fn integer_range_iterate() {
2759 let t = type_at(
2760 r#"
2761//- /main.rs crate:main deps:std
2762fn test() {
2763 for x in 0..100 { x<|>; }
2764}
2765
2766//- /std.rs crate:std
2767pub mod ops {
2768 pub struct Range<Idx> {
2769 pub start: Idx,
2770 pub end: Idx,
2771 }
2772}
2773
2774pub mod iter {
2775 pub trait Iterator {
2776 type Item;
2777 }
2778
2779 pub trait IntoIterator {
2780 type Item;
2781 type IntoIter: Iterator<Item = Self::Item>;
2782 }
2783
2784 impl<T> IntoIterator for T where T: Iterator {
2785 type Item = <T as Iterator>::Item;
2786 type IntoIter = Self;
2787 }
2788}
2789
2790trait Step {}
2791impl Step for i32 {}
2792impl Step for i64 {}
2793
2794impl<A: Step> iter::Iterator for ops::Range<A> {
2795 type Item = A;
2796}
2797"#,
2798 );
2799 assert_eq!(t, "i32");
2800}