mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rust: Add type inference tests and rename modules
This commit is contained in:
@@ -1,4 +1,69 @@
|
||||
mod m1 {
|
||||
mod field_access {
|
||||
#[derive(Debug)]
|
||||
struct S;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MyThing {
|
||||
a: S,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum MyOption<T> {
|
||||
MyNone(),
|
||||
MySome(T),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct GenericThing<A> {
|
||||
a: A,
|
||||
}
|
||||
|
||||
struct OptionS {
|
||||
a: MyOption<S>,
|
||||
}
|
||||
|
||||
fn simple_field_access() {
|
||||
let x = MyThing { a: S };
|
||||
println!("{:?}", x.a);
|
||||
}
|
||||
|
||||
fn generic_field_access() {
|
||||
// Explicit type argument
|
||||
let x = GenericThing::<S> { a: S };
|
||||
println!("{:?}", x.a);
|
||||
|
||||
// Implicit type argument
|
||||
let y = GenericThing { a: S };
|
||||
println!("{:?}", x.a);
|
||||
|
||||
// The type of the field `a` can only be infered from the concrete type
|
||||
// in the struct declaration.
|
||||
let x = OptionS {
|
||||
a: MyOption::MyNone(),
|
||||
};
|
||||
println!("{:?}", x.a);
|
||||
|
||||
// The type of the field `a` can only be infered from the type argument
|
||||
let x = GenericThing::<MyOption<S>> {
|
||||
a: MyOption::MyNone(),
|
||||
};
|
||||
println!("{:?}", x.a);
|
||||
|
||||
let mut x = GenericThing {
|
||||
a: MyOption::MyNone(),
|
||||
};
|
||||
// Only after this access can we infer the type parameter of `x`
|
||||
let a: MyOption<S> = x.a;
|
||||
println!("{:?}", a);
|
||||
}
|
||||
|
||||
pub fn f() {
|
||||
simple_field_access();
|
||||
generic_field_access();
|
||||
}
|
||||
}
|
||||
|
||||
mod method_impl {
|
||||
pub struct Foo {}
|
||||
|
||||
impl Foo {
|
||||
@@ -25,7 +90,7 @@ mod m1 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m2 {
|
||||
mod method_non_parametric_impl {
|
||||
#[derive(Debug)]
|
||||
struct MyThing<A> {
|
||||
a: A,
|
||||
@@ -58,6 +123,10 @@ mod m2 {
|
||||
let x = MyThing { a: S1 };
|
||||
let y = MyThing { a: S2 };
|
||||
|
||||
// simple field access
|
||||
println!("{:?}", x.a);
|
||||
println!("{:?}", y.a);
|
||||
|
||||
println!("{:?}", x.m1()); // missing call target
|
||||
println!("{:?}", y.m1().a); // missing call target
|
||||
|
||||
@@ -69,7 +138,7 @@ mod m2 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m3 {
|
||||
mod method_non_parametric_trait_impl {
|
||||
#[derive(Debug)]
|
||||
struct MyThing<A> {
|
||||
a: A,
|
||||
@@ -122,7 +191,7 @@ mod m3 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m4 {
|
||||
mod function_trait_bounds {
|
||||
#[derive(Debug)]
|
||||
struct MyThing<A> {
|
||||
a: A,
|
||||
@@ -175,7 +244,7 @@ mod m4 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m5 {
|
||||
mod trait_associated_type {
|
||||
trait MyTrait {
|
||||
type AssociatedType;
|
||||
|
||||
@@ -210,7 +279,7 @@ mod m5 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m6 {
|
||||
mod generic_enum {
|
||||
#[derive(Debug)]
|
||||
enum MyEnum<A> {
|
||||
C1(A),
|
||||
@@ -240,7 +309,7 @@ mod m6 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m7 {
|
||||
mod method_supertraits {
|
||||
#[derive(Debug)]
|
||||
struct MyThing<A> {
|
||||
a: A,
|
||||
@@ -325,7 +394,7 @@ mod m7 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m8 {
|
||||
mod function_trait_bounds_2 {
|
||||
use std::convert::From;
|
||||
use std::fmt::Debug;
|
||||
|
||||
@@ -374,7 +443,7 @@ mod m8 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m9 {
|
||||
mod option_methods {
|
||||
#[derive(Debug)]
|
||||
enum MyOption<T> {
|
||||
MyNone(),
|
||||
@@ -456,7 +525,7 @@ mod m9 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m10 {
|
||||
mod method_call_type_conversion {
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct S<T>(T);
|
||||
@@ -508,7 +577,7 @@ mod m10 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m11 {
|
||||
mod trait_implicit_self_borrow {
|
||||
trait MyTrait {
|
||||
fn foo(&self) -> &Self;
|
||||
|
||||
@@ -531,7 +600,7 @@ mod m11 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m12 {
|
||||
mod implicit_self_borrow {
|
||||
struct S;
|
||||
|
||||
struct MyStruct<T>(T);
|
||||
@@ -548,7 +617,7 @@ mod m12 {
|
||||
}
|
||||
}
|
||||
|
||||
mod m13 {
|
||||
mod borrowed_typed {
|
||||
struct S;
|
||||
|
||||
impl S {
|
||||
@@ -578,18 +647,19 @@ mod m13 {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
m1::f();
|
||||
m1::g(m1::Foo {}, m1::Foo {});
|
||||
m2::f();
|
||||
m3::f();
|
||||
m4::f();
|
||||
m5::f();
|
||||
m6::f();
|
||||
m7::f();
|
||||
m8::f();
|
||||
m9::f();
|
||||
m10::f();
|
||||
m11::f();
|
||||
m12::f();
|
||||
m13::f();
|
||||
field_access::f();
|
||||
method_impl::f();
|
||||
method_impl::g(method_impl::Foo {}, method_impl::Foo {});
|
||||
method_non_parametric_impl::f();
|
||||
method_non_parametric_trait_impl::f();
|
||||
function_trait_bounds::f();
|
||||
trait_associated_type::f();
|
||||
generic_enum::f();
|
||||
method_supertraits::f();
|
||||
function_trait_bounds_2::f();
|
||||
option_methods::f();
|
||||
method_call_type_conversion::f();
|
||||
trait_implicit_self_borrow::f();
|
||||
implicit_self_borrow::f();
|
||||
borrowed_typed::f();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user