Skip to content

Commit c5987b4

Browse files
authored
Merge pull request #21019 from paldepind/rust/impl-return
Rust: Don't propagate `impl` in return position into function bodies
2 parents f8c144b + 4425891 commit c5987b4

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,6 @@ private TypeMention getTypeAnnotation(AstNode n) {
267267
n = p.getPat() and
268268
result = p.getTypeRepr()
269269
)
270-
or
271-
exists(Function f |
272-
result = getReturnTypeMention(f) and
273-
n = f.getFunctionBody()
274-
)
275270
}
276271

277272
/** Gets the type of `n`, which has an explicit type annotation. */
@@ -282,6 +277,17 @@ private Type inferAnnotatedType(AstNode n, TypePath path) {
282277
result = n.(ShorthandSelfParameterMention).resolveTypeAt(path)
283278
}
284279

280+
pragma[nomagic]
281+
private Type inferFunctionBodyType(AstNode n, TypePath path) {
282+
exists(Function f |
283+
n = f.getFunctionBody() and
284+
result = getReturnTypeMention(f).resolveTypeAt(path) and
285+
not exists(ImplTraitReturnType i | i.getFunction() = f |
286+
result = i or result = i.getATypeParameter()
287+
)
288+
)
289+
}
290+
285291
/**
286292
* Holds if `me` is a call to the `panic!` macro.
287293
*
@@ -419,6 +425,8 @@ module CertainTypeInference {
419425
result = inferAnnotatedType(n, path) and
420426
Stages::TypeInferenceStage::ref()
421427
or
428+
result = inferFunctionBodyType(n, path)
429+
or
422430
result = inferCertainCallExprType(n, path)
423431
or
424432
result = inferCertainTypeEquality(n, path)

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,9 +2754,7 @@ inferCertainType
27542754
| main.rs:2224:18:2224:21 | SelfParam | | main.rs:2221:5:2221:14 | S1 |
27552755
| main.rs:2224:24:2224:25 | { ... } | | {EXTERNAL LOCATION} | () |
27562756
| main.rs:2227:25:2229:5 | { ... } | | main.rs:2221:5:2221:14 | S1 |
2757-
| main.rs:2231:41:2233:5 | { ... } | | main.rs:2231:16:2231:39 | impl ... |
27582757
| main.rs:2232:9:2232:20 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
2759-
| main.rs:2235:41:2237:5 | { ... } | | main.rs:2235:16:2235:39 | impl ... |
27602758
| main.rs:2236:9:2236:16 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
27612759
| main.rs:2236:9:2236:16 | { ... } | dyn(Output) | {EXTERNAL LOCATION} | () |
27622760
| main.rs:2245:13:2245:42 | SelfParam | | {EXTERNAL LOCATION} | Pin |
@@ -2766,7 +2764,6 @@ inferCertainType
27662764
| main.rs:2246:13:2246:15 | _cx | TRef | {EXTERNAL LOCATION} | Context |
27672765
| main.rs:2247:44:2249:9 | { ... } | | {EXTERNAL LOCATION} | Poll |
27682766
| main.rs:2247:44:2249:9 | { ... } | T | main.rs:2221:5:2221:14 | S1 |
2769-
| main.rs:2252:41:2254:5 | { ... } | | main.rs:2252:16:2252:39 | impl ... |
27702767
| main.rs:2256:22:2264:5 | { ... } | | {EXTERNAL LOCATION} | () |
27712768
| main.rs:2257:9:2257:12 | f1(...) | | {EXTERNAL LOCATION} | dyn Future |
27722769
| main.rs:2257:9:2257:12 | f1(...) | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
@@ -2788,7 +2785,6 @@ inferCertainType
27882785
| main.rs:2286:15:2286:19 | SelfParam | | {EXTERNAL LOCATION} | & |
27892786
| main.rs:2286:15:2286:19 | SelfParam | TRef | main.rs:2268:5:2269:14 | S1 |
27902787
| main.rs:2286:22:2286:23 | { ... } | | {EXTERNAL LOCATION} | () |
2791-
| main.rs:2289:37:2291:5 | { ... } | | main.rs:2289:16:2289:35 | impl ... + ... |
27922788
| main.rs:2294:18:2294:22 | SelfParam | | {EXTERNAL LOCATION} | & |
27932789
| main.rs:2294:18:2294:22 | SelfParam | TRef | main.rs:2293:5:2295:5 | Self [trait MyTrait] |
27942790
| main.rs:2298:18:2298:22 | SelfParam | | {EXTERNAL LOCATION} | & |
@@ -2801,7 +2797,6 @@ inferCertainType
28012797
| main.rs:2305:25:2305:28 | self | | {EXTERNAL LOCATION} | & |
28022798
| main.rs:2305:25:2305:28 | self | TRef | main.rs:2271:5:2271:22 | S3 |
28032799
| main.rs:2305:25:2305:28 | self | TRef.T3 | main.rs:2303:10:2303:17 | T |
2804-
| main.rs:2310:45:2312:5 | { ... } | | main.rs:2310:28:2310:43 | impl ... |
28052800
| main.rs:2314:41:2314:41 | t | | main.rs:2314:26:2314:38 | B |
28062801
| main.rs:2314:52:2316:5 | { ... } | | main.rs:2314:23:2314:23 | A |
28072802
| main.rs:2315:9:2315:9 | t | | main.rs:2314:26:2314:38 | B |
@@ -9386,11 +9381,13 @@ inferType
93869381
| main.rs:2224:24:2224:25 | { ... } | | {EXTERNAL LOCATION} | () |
93879382
| main.rs:2227:25:2229:5 | { ... } | | main.rs:2221:5:2221:14 | S1 |
93889383
| main.rs:2228:9:2228:10 | S1 | | main.rs:2221:5:2221:14 | S1 |
9389-
| main.rs:2231:41:2233:5 | { ... } | | main.rs:2231:16:2231:39 | impl ... |
9384+
| main.rs:2231:41:2233:5 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
9385+
| main.rs:2231:41:2233:5 | { ... } | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
93909386
| main.rs:2232:9:2232:20 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
93919387
| main.rs:2232:9:2232:20 | { ... } | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
93929388
| main.rs:2232:17:2232:18 | S1 | | main.rs:2221:5:2221:14 | S1 |
9393-
| main.rs:2235:41:2237:5 | { ... } | | main.rs:2235:16:2235:39 | impl ... |
9389+
| main.rs:2235:41:2237:5 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
9390+
| main.rs:2235:41:2237:5 | { ... } | dyn(Output) | {EXTERNAL LOCATION} | () |
93949391
| main.rs:2236:9:2236:16 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
93959392
| main.rs:2236:9:2236:16 | { ... } | dyn(Output) | {EXTERNAL LOCATION} | () |
93969393
| main.rs:2245:13:2245:42 | SelfParam | | {EXTERNAL LOCATION} | Pin |
@@ -9403,9 +9400,8 @@ inferType
94039400
| main.rs:2248:13:2248:38 | ...::Ready(...) | | {EXTERNAL LOCATION} | Poll |
94049401
| main.rs:2248:13:2248:38 | ...::Ready(...) | T | main.rs:2221:5:2221:14 | S1 |
94059402
| main.rs:2248:36:2248:37 | S1 | | main.rs:2221:5:2221:14 | S1 |
9406-
| main.rs:2252:41:2254:5 | { ... } | | main.rs:2252:16:2252:39 | impl ... |
9403+
| main.rs:2252:41:2254:5 | { ... } | | main.rs:2239:5:2239:14 | S2 |
94079404
| main.rs:2253:9:2253:10 | S2 | | main.rs:2239:5:2239:14 | S2 |
9408-
| main.rs:2253:9:2253:10 | S2 | | main.rs:2252:16:2252:39 | impl ... |
94099405
| main.rs:2256:22:2264:5 | { ... } | | {EXTERNAL LOCATION} | () |
94109406
| main.rs:2257:9:2257:12 | f1(...) | | {EXTERNAL LOCATION} | dyn Future |
94119407
| main.rs:2257:9:2257:12 | f1(...) | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
@@ -9443,9 +9439,8 @@ inferType
94439439
| main.rs:2286:15:2286:19 | SelfParam | | {EXTERNAL LOCATION} | & |
94449440
| main.rs:2286:15:2286:19 | SelfParam | TRef | main.rs:2268:5:2269:14 | S1 |
94459441
| main.rs:2286:22:2286:23 | { ... } | | {EXTERNAL LOCATION} | () |
9446-
| main.rs:2289:37:2291:5 | { ... } | | main.rs:2289:16:2289:35 | impl ... + ... |
9442+
| main.rs:2289:37:2291:5 | { ... } | | main.rs:2268:5:2269:14 | S1 |
94479443
| main.rs:2290:9:2290:10 | S1 | | main.rs:2268:5:2269:14 | S1 |
9448-
| main.rs:2290:9:2290:10 | S1 | | main.rs:2289:16:2289:35 | impl ... + ... |
94499444
| main.rs:2294:18:2294:22 | SelfParam | | {EXTERNAL LOCATION} | & |
94509445
| main.rs:2294:18:2294:22 | SelfParam | TRef | main.rs:2293:5:2295:5 | Self [trait MyTrait] |
94519446
| main.rs:2298:18:2298:22 | SelfParam | | {EXTERNAL LOCATION} | & |
@@ -9464,9 +9459,8 @@ inferType
94649459
| main.rs:2305:25:2305:28 | self | TRef | main.rs:2271:5:2271:22 | S3 |
94659460
| main.rs:2305:25:2305:28 | self | TRef.T3 | main.rs:2303:10:2303:17 | T |
94669461
| main.rs:2306:13:2306:21 | t.clone() | | main.rs:2303:10:2303:17 | T |
9467-
| main.rs:2310:45:2312:5 | { ... } | | main.rs:2310:28:2310:43 | impl ... |
9462+
| main.rs:2310:45:2312:5 | { ... } | | main.rs:2268:5:2269:14 | S1 |
94689463
| main.rs:2311:9:2311:10 | S1 | | main.rs:2268:5:2269:14 | S1 |
9469-
| main.rs:2311:9:2311:10 | S1 | | main.rs:2310:28:2310:43 | impl ... |
94709464
| main.rs:2314:41:2314:41 | t | | main.rs:2314:26:2314:38 | B |
94719465
| main.rs:2314:52:2316:5 | { ... } | | main.rs:2314:23:2314:23 | A |
94729466
| main.rs:2315:9:2315:9 | t | | main.rs:2314:26:2314:38 | B |

0 commit comments

Comments
 (0)