diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-24 22:37:48 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-24 22:37:48 +0000 |
commit | d7db38fff9c251c36d0796309b43678bdf9e5bd8 (patch) | |
tree | 83b422d45c28e3ae9a2eac550ce8a6048bf5fd71 /crates/hir_ty/src/tests | |
parent | 9d81618f11eb403cc0644a22f30648393ec77cf6 (diff) | |
parent | d1156bb52e900c015afd490f509d744c7a5adf10 (diff) |
Merge #7907
7907: Autoderef with visibility r=cynecx a=cynecx
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/7841.
I am not sure about the general approach here. Right now this simply tries to check whether the autoderef candidate is reachable from the current module. ~~However this doesn't exactly work with traits (see the `tests::macros::infer_derive_clone_in_core` test, which fails right now).~~ see comment below
Refs:
- `rustc_typeck` checking fields: https://github.com/rust-lang/rust/blob/66ec64ccf31883cd2c28d045912a76179c0c6ed2/compiler/rustc_typeck/src/check/expr.rs#L1610
r? @flodiebold
Co-authored-by: cynecx <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/tests')
-rw-r--r-- | crates/hir_ty/src/tests/macros.rs | 4 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/method_resolution.rs | 119 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/simple.rs | 32 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/traits.rs | 4 |
4 files changed, 139 insertions, 20 deletions
diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index 12951fb16..7eda51866 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs | |||
@@ -31,12 +31,12 @@ struct S; | |||
31 | 31 | ||
32 | #[cfg(not(test))] | 32 | #[cfg(not(test))] |
33 | impl S { | 33 | impl S { |
34 | fn foo3(&self) -> i32 { 0 } | 34 | pub fn foo3(&self) -> i32 { 0 } |
35 | } | 35 | } |
36 | 36 | ||
37 | #[cfg(test)] | 37 | #[cfg(test)] |
38 | impl S { | 38 | impl S { |
39 | fn foo4(&self) -> i32 { 0 } | 39 | pub fn foo4(&self) -> i32 { 0 } |
40 | } | 40 | } |
41 | "#, | 41 | "#, |
42 | ); | 42 | ); |
diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs index 4e3f9a9b6..61f18b0d2 100644 --- a/crates/hir_ty/src/tests/method_resolution.rs +++ b/crates/hir_ty/src/tests/method_resolution.rs | |||
@@ -1173,3 +1173,122 @@ fn main() { | |||
1173 | "#, | 1173 | "#, |
1174 | ); | 1174 | ); |
1175 | } | 1175 | } |
1176 | |||
1177 | #[test] | ||
1178 | fn autoderef_visibility_field() { | ||
1179 | check_infer( | ||
1180 | r#" | ||
1181 | #[lang = "deref"] | ||
1182 | pub trait Deref { | ||
1183 | type Target; | ||
1184 | fn deref(&self) -> &Self::Target; | ||
1185 | } | ||
1186 | mod a { | ||
1187 | pub struct Foo(pub char); | ||
1188 | pub struct Bar(i32); | ||
1189 | impl Bar { | ||
1190 | pub fn new() -> Self { | ||
1191 | Self(0) | ||
1192 | } | ||
1193 | } | ||
1194 | impl super::Deref for Bar { | ||
1195 | type Target = Foo; | ||
1196 | fn deref(&self) -> &Foo { | ||
1197 | &Foo('z') | ||
1198 | } | ||
1199 | } | ||
1200 | } | ||
1201 | mod b { | ||
1202 | fn foo() { | ||
1203 | let x = super::a::Bar::new().0; | ||
1204 | } | ||
1205 | } | ||
1206 | "#, | ||
1207 | expect![[r#" | ||
1208 | 67..71 'self': &Self | ||
1209 | 200..231 '{ ... }': Bar | ||
1210 | 214..218 'Self': Bar(i32) -> Bar | ||
1211 | 214..221 'Self(0)': Bar | ||
1212 | 219..220 '0': i32 | ||
1213 | 315..319 'self': &Bar | ||
1214 | 329..362 '{ ... }': &Foo | ||
1215 | 343..352 '&Foo('z')': &Foo | ||
1216 | 344..347 'Foo': Foo(char) -> Foo | ||
1217 | 344..352 'Foo('z')': Foo | ||
1218 | 348..351 ''z'': char | ||
1219 | 392..439 '{ ... }': () | ||
1220 | 406..407 'x': char | ||
1221 | 410..428 'super:...r::new': fn new() -> Bar | ||
1222 | 410..430 'super:...:new()': Bar | ||
1223 | 410..432 'super:...ew().0': char | ||
1224 | "#]], | ||
1225 | ) | ||
1226 | } | ||
1227 | |||
1228 | #[test] | ||
1229 | fn autoderef_visibility_method() { | ||
1230 | cov_mark::check!(autoderef_candidate_not_visible); | ||
1231 | check_infer( | ||
1232 | r#" | ||
1233 | #[lang = "deref"] | ||
1234 | pub trait Deref { | ||
1235 | type Target; | ||
1236 | fn deref(&self) -> &Self::Target; | ||
1237 | } | ||
1238 | mod a { | ||
1239 | pub struct Foo(pub char); | ||
1240 | impl Foo { | ||
1241 | pub fn mango(&self) -> char { | ||
1242 | self.0 | ||
1243 | } | ||
1244 | } | ||
1245 | pub struct Bar(i32); | ||
1246 | impl Bar { | ||
1247 | pub fn new() -> Self { | ||
1248 | Self(0) | ||
1249 | } | ||
1250 | fn mango(&self) -> i32 { | ||
1251 | self.0 | ||
1252 | } | ||
1253 | } | ||
1254 | impl super::Deref for Bar { | ||
1255 | type Target = Foo; | ||
1256 | fn deref(&self) -> &Foo { | ||
1257 | &Foo('z') | ||
1258 | } | ||
1259 | } | ||
1260 | } | ||
1261 | mod b { | ||
1262 | fn foo() { | ||
1263 | let x = super::a::Bar::new().mango(); | ||
1264 | } | ||
1265 | } | ||
1266 | "#, | ||
1267 | expect![[r#" | ||
1268 | 67..71 'self': &Self | ||
1269 | 168..172 'self': &Foo | ||
1270 | 182..212 '{ ... }': char | ||
1271 | 196..200 'self': &Foo | ||
1272 | 196..202 'self.0': char | ||
1273 | 288..319 '{ ... }': Bar | ||
1274 | 302..306 'Self': Bar(i32) -> Bar | ||
1275 | 302..309 'Self(0)': Bar | ||
1276 | 307..308 '0': i32 | ||
1277 | 338..342 'self': &Bar | ||
1278 | 351..381 '{ ... }': i32 | ||
1279 | 365..369 'self': &Bar | ||
1280 | 365..371 'self.0': i32 | ||
1281 | 465..469 'self': &Bar | ||
1282 | 479..512 '{ ... }': &Foo | ||
1283 | 493..502 '&Foo('z')': &Foo | ||
1284 | 494..497 'Foo': Foo(char) -> Foo | ||
1285 | 494..502 'Foo('z')': Foo | ||
1286 | 498..501 ''z'': char | ||
1287 | 542..595 '{ ... }': () | ||
1288 | 556..557 'x': char | ||
1289 | 560..578 'super:...r::new': fn new() -> Bar | ||
1290 | 560..580 'super:...:new()': Bar | ||
1291 | 560..588 'super:...ango()': char | ||
1292 | "#]], | ||
1293 | ) | ||
1294 | } | ||
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index bcc43ed70..361cd6302 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs | |||
@@ -1103,7 +1103,7 @@ fn infer_inherent_method() { | |||
1103 | 1103 | ||
1104 | mod b { | 1104 | mod b { |
1105 | impl super::A { | 1105 | impl super::A { |
1106 | fn bar(&self, x: u64) -> i64 {} | 1106 | pub fn bar(&self, x: u64) -> i64 {} |
1107 | } | 1107 | } |
1108 | } | 1108 | } |
1109 | 1109 | ||
@@ -1117,21 +1117,21 @@ fn infer_inherent_method() { | |||
1117 | 31..35 'self': A | 1117 | 31..35 'self': A |
1118 | 37..38 'x': u32 | 1118 | 37..38 'x': u32 |
1119 | 52..54 '{}': () | 1119 | 52..54 '{}': () |
1120 | 102..106 'self': &A | 1120 | 106..110 'self': &A |
1121 | 108..109 'x': u64 | 1121 | 112..113 'x': u64 |
1122 | 123..125 '{}': () | 1122 | 127..129 '{}': () |
1123 | 143..144 'a': A | 1123 | 147..148 'a': A |
1124 | 149..197 '{ ...(1); }': () | 1124 | 153..201 '{ ...(1); }': () |
1125 | 155..156 'a': A | 1125 | 159..160 'a': A |
1126 | 155..163 'a.foo(1)': i32 | 1126 | 159..167 'a.foo(1)': i32 |
1127 | 161..162 '1': u32 | 1127 | 165..166 '1': u32 |
1128 | 169..180 '(&a).bar(1)': i64 | 1128 | 173..184 '(&a).bar(1)': i64 |
1129 | 170..172 '&a': &A | 1129 | 174..176 '&a': &A |
1130 | 171..172 'a': A | 1130 | 175..176 'a': A |
1131 | 178..179 '1': u64 | 1131 | 182..183 '1': u64 |
1132 | 186..187 'a': A | 1132 | 190..191 'a': A |
1133 | 186..194 'a.bar(1)': i64 | 1133 | 190..198 'a.bar(1)': i64 |
1134 | 192..193 '1': u64 | 1134 | 196..197 '1': u64 |
1135 | "#]], | 1135 | "#]], |
1136 | ); | 1136 | ); |
1137 | } | 1137 | } |
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 45a1958e3..2ba97f814 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs | |||
@@ -187,8 +187,8 @@ mod iter { | |||
187 | mod collections { | 187 | mod collections { |
188 | struct Vec<T> {} | 188 | struct Vec<T> {} |
189 | impl<T> Vec<T> { | 189 | impl<T> Vec<T> { |
190 | fn new() -> Self { Vec {} } | 190 | pub fn new() -> Self { Vec {} } |
191 | fn push(&mut self, t: T) { } | 191 | pub fn push(&mut self, t: T) { } |
192 | } | 192 | } |
193 | 193 | ||
194 | impl<T> IntoIterator for Vec<T> { | 194 | impl<T> IntoIterator for Vec<T> { |