Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion rust/ql/lib/codeql/rust/internal/TypeMention.qll
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ class TypeReprMention extends TypeMention, TypeRepr {
or
result = this.(PathTypeRepr).getPath().(PathMention).resolveType()
}

override Type resolveTypeAt(TypePath path) {
result = this.(PathTypeRepr).getPath().(PathMention).resolveTypeAt(path)
or
not exists(this.(PathTypeRepr).getPath()) and
result = super.resolveTypeAt(path)
}
}

/** Holds if `path` resolves the type alias `alias` with the definition `rhs`. */
private predicate resolvePathAlias(Path path, TypeAlias alias, TypeReprMention rhs) {
alias = resolvePath(path) and rhs = alias.getTypeRepr()
}

class PathMention extends TypeMention, Path {
abstract class PathMention extends TypeMention, Path { }

class NonAliasPathMention extends PathMention {
NonAliasPathMention() { not resolvePathAlias(this, _, _) }

override TypeMention getTypeArgument(int i) {
result = this.getSegment().getGenericArgList().getTypeArg(i)
or
Expand Down Expand Up @@ -98,6 +114,37 @@ class PathMention extends TypeMention, Path {
}
}

class AliasPathMention extends PathMention {
TypeAlias alias;
TypeReprMention rhs;

AliasPathMention() { resolvePathAlias(this, alias, rhs) }

override TypeMention getTypeArgument(int i) {
Comment thread
hvitved marked this conversation as resolved.
Outdated
result = this.getSegment().getGenericArgList().getTypeArg(i)
}

/** Get the `i`th type parameter of the alias itself. */
private TypeParameter getTypeParameter(int i) {
result = TTypeParamTypeParameter(alias.getGenericParamList().getTypeParam(i))
}

override Type resolveType() { result = rhs.resolveType() }

override Type resolveTypeAt(TypePath path) {
result = rhs.resolveTypeAt(path) and
not result = this.getTypeParameter(_)
or
exists(TypeParameter tp, TypeMention arg, TypePath prefix, TypePath suffix, int i |
tp = rhs.resolveTypeAt(prefix) and
tp = this.getTypeParameter(i) and
arg = this.getTypeArgument(i) and
result = arg.resolveTypeAt(suffix) and
path = prefix.append(suffix)
)
}
}

// Used to represent implicit `Self` type arguments in traits and `impl` blocks,
// see `PathMention` for details.
class TypeParamMention extends TypeMention, TypeParam {
Expand Down
10 changes: 5 additions & 5 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ mod type_aliases {
// Alias to another alias
type AliasToAlias<A4> = AnotherPair<A4>;

// Alias that appears nested withing another alias
// Alias that appears nested within another alias
type NestedAlias<A5> = AnotherPair<AliasToAlias<A5>>;

fn g(t: NestedAlias<S3>) {
let x = t.unwrapSnd().unwrapSnd(); // $ method=unwrapSnd MISSING: type=x:S3
let x = t.unwrapSnd().unwrapSnd(); // $ method=unwrapSnd type=x:S3
println!("{:?}", x);
}

Expand All @@ -583,15 +583,15 @@ mod type_aliases {
println!("{:?}", p1);

// Type can be only inferred from the type alias
let p2: MyPair = PairOption::PairNone(); // $ MISSING: type=p2:Fst.S1 MISSING: type=p2:Snd.S2
let p2: MyPair = PairOption::PairNone(); // $ type=p2:Fst.S1 type=p2:Snd.S2
println!("{:?}", p2);

// First type from alias, second from constructor
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ MISSING: type=p3:Fst.S2
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ type=p3:Fst.S2
println!("{:?}", p3);

// First type from alias definition, second from argument to alias
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ SPURIOUS: type=p3:Fst.S3 MISSING: type=p3:Snd.S3
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ type=p3:Fst.S2 type=p3:Snd.S3
println!("{:?}", p3);

g(PairOption::PairSnd(PairOption::PairSnd(S3)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,21 @@ inferType
| main.rs:549:41:549:43 | snd | | main.rs:543:15:543:17 | Snd |
| main.rs:549:49:549:51 | snd | | main.rs:543:15:543:17 | Snd |
| main.rs:575:10:575:10 | t | | main.rs:535:5:541:5 | PairOption |
| main.rs:575:10:575:10 | t | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:575:10:575:10 | t | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:575:10:575:10 | t | Snd | main.rs:535:5:541:5 | PairOption |
| main.rs:575:10:575:10 | t | Snd.Fst | main.rs:557:5:558:14 | S2 |
| main.rs:575:10:575:10 | t | Snd.Snd | main.rs:560:5:561:14 | S3 |
| main.rs:576:13:576:13 | x | | main.rs:560:5:561:14 | S3 |
| main.rs:576:17:576:17 | t | | main.rs:535:5:541:5 | PairOption |
| main.rs:576:17:576:17 | t | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:576:17:576:17 | t | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:576:17:576:17 | t | Snd | main.rs:535:5:541:5 | PairOption |
| main.rs:576:17:576:17 | t | Snd.Fst | main.rs:557:5:558:14 | S2 |
| main.rs:576:17:576:17 | t | Snd.Snd | main.rs:560:5:561:14 | S3 |
| main.rs:576:17:576:29 | t.unwrapSnd() | | main.rs:535:5:541:5 | PairOption |
| main.rs:576:17:576:29 | t.unwrapSnd() | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:576:17:576:29 | t.unwrapSnd() | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:576:17:576:41 | ... .unwrapSnd() | | main.rs:560:5:561:14 | S3 |
| main.rs:577:26:577:26 | x | | main.rs:560:5:561:14 | S3 |
| main.rs:582:13:582:14 | p1 | | main.rs:535:5:541:5 | PairOption |
| main.rs:582:13:582:14 | p1 | Fst | main.rs:554:5:555:14 | S1 |
| main.rs:582:13:582:14 | p1 | Snd | main.rs:557:5:558:14 | S2 |
Expand All @@ -525,26 +537,40 @@ inferType
| main.rs:583:26:583:27 | p1 | Fst | main.rs:554:5:555:14 | S1 |
| main.rs:583:26:583:27 | p1 | Snd | main.rs:557:5:558:14 | S2 |
| main.rs:586:13:586:14 | p2 | | main.rs:535:5:541:5 | PairOption |
| main.rs:586:13:586:14 | p2 | Fst | main.rs:554:5:555:14 | S1 |
| main.rs:586:13:586:14 | p2 | Snd | main.rs:557:5:558:14 | S2 |
| main.rs:586:26:586:47 | ...::PairNone(...) | | main.rs:535:5:541:5 | PairOption |
| main.rs:586:26:586:47 | ...::PairNone(...) | Fst | main.rs:554:5:555:14 | S1 |
| main.rs:586:26:586:47 | ...::PairNone(...) | Snd | main.rs:557:5:558:14 | S2 |
| main.rs:587:26:587:27 | p2 | | main.rs:535:5:541:5 | PairOption |
| main.rs:587:26:587:27 | p2 | Fst | main.rs:554:5:555:14 | S1 |
| main.rs:587:26:587:27 | p2 | Snd | main.rs:557:5:558:14 | S2 |
| main.rs:590:13:590:14 | p3 | | main.rs:535:5:541:5 | PairOption |
| main.rs:590:13:590:14 | p3 | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:590:13:590:14 | p3 | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:590:34:590:56 | ...::PairSnd(...) | | main.rs:535:5:541:5 | PairOption |
| main.rs:590:34:590:56 | ...::PairSnd(...) | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:590:34:590:56 | ...::PairSnd(...) | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:590:54:590:55 | S3 | | main.rs:560:5:561:14 | S3 |
| main.rs:591:26:591:27 | p3 | | main.rs:535:5:541:5 | PairOption |
| main.rs:591:26:591:27 | p3 | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:591:26:591:27 | p3 | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:594:13:594:14 | p3 | | main.rs:535:5:541:5 | PairOption |
| main.rs:594:13:594:14 | p3 | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:594:13:594:14 | p3 | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:594:13:594:14 | p3 | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:594:35:594:56 | ...::PairNone(...) | | main.rs:535:5:541:5 | PairOption |
| main.rs:594:35:594:56 | ...::PairNone(...) | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:594:35:594:56 | ...::PairNone(...) | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:594:35:594:56 | ...::PairNone(...) | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:595:26:595:27 | p3 | | main.rs:535:5:541:5 | PairOption |
| main.rs:595:26:595:27 | p3 | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:595:26:595:27 | p3 | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:595:26:595:27 | p3 | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:597:11:597:54 | ...::PairSnd(...) | | main.rs:535:5:541:5 | PairOption |
| main.rs:597:11:597:54 | ...::PairSnd(...) | Fst | main.rs:560:5:561:14 | S3 |
| main.rs:597:11:597:54 | ...::PairSnd(...) | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:597:11:597:54 | ...::PairSnd(...) | Snd | main.rs:535:5:541:5 | PairOption |
| main.rs:597:11:597:54 | ...::PairSnd(...) | Snd.Fst | main.rs:557:5:558:14 | S2 |
| main.rs:597:11:597:54 | ...::PairSnd(...) | Snd.Snd | main.rs:560:5:561:14 | S3 |
| main.rs:597:31:597:53 | ...::PairSnd(...) | | main.rs:535:5:541:5 | PairOption |
| main.rs:597:31:597:53 | ...::PairSnd(...) | Fst | main.rs:557:5:558:14 | S2 |
| main.rs:597:31:597:53 | ...::PairSnd(...) | Snd | main.rs:560:5:561:14 | S3 |
| main.rs:597:51:597:52 | S3 | | main.rs:560:5:561:14 | S3 |
| main.rs:610:16:610:24 | SelfParam | | file://:0:0:0:0 | & |
Expand Down