Skip to content

Commit

Permalink
Add some more error handling to generics
Browse files Browse the repository at this point in the history
Check for wrong number of arguments and fix crash with invalid Type
specified as generic argument.
  • Loading branch information
philberty committed Mar 1, 2021
1 parent ec9ead4 commit 1c6eaa1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
10 changes: 7 additions & 3 deletions gcc/rust/resolve/rust-ast-resolve-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ class ResolveType : public ResolverBase
{
resolved_node = ResolveTypePath::go (path, parent);
ok = resolved_node != UNKNOWN_NODEID;
resolver->insert_resolved_type (path.get_node_id (), resolved_node);
resolver->insert_new_definition (path.get_node_id (),
Definition{path.get_node_id (), parent});
if (ok)
{
resolver->insert_resolved_type (path.get_node_id (), resolved_node);
resolver->insert_new_definition (path.get_node_id (),
Definition{path.get_node_id (),
parent});
}
}

void visit (AST::ArrayType &type)
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
else
{
rust_error_at (expr->get_locus (), "unknown path %s",
expr->as_string ().c_str (), path_buf.c_str ());
expr->as_string ().c_str ());
}
}

Expand Down
11 changes: 9 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ class TypeCheckExpr : public TypeCheckBase
if (function_tyty == nullptr)
return;

bool valid_tyty = function_tyty->get_kind () == TyTy::TypeKind::ADT
|| function_tyty->get_kind () == TyTy::TypeKind::FNDEF;
if (!valid_tyty)
{
rust_error_at (expr.get_locus (),
"Failed to resolve expression of function call");
return;
}

infered = TyTy::TypeCheckCallExpr::go (function_tyty, expr, context);
if (infered == nullptr)
{
Expand Down Expand Up @@ -778,8 +787,6 @@ class TypeCheckExpr : public TypeCheckBase
? adt->handle_substitions (seg.get_generic_args ())
: adt->infer_substitions ();
}

context->insert_type (expr.get_mappings (), infered->clone ());
}

void visit (HIR::LoopExpr &expr)
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-hir-type-check-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class TypeCheckType : public TypeCheckBase
}
}

rust_error_at (path.get_locus (), "failed to resolve TypePath: %s",
rust_error_at (path.get_locus (), "failed to type-resolve TypePath: %s",
path.as_string ().c_str ());
}

Expand Down
9 changes: 9 additions & 0 deletions gcc/testsuite/rust.test/fail_compilation/generics1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct GenericStruct<T>(T, usize);

fn main() {
let a2: GenericStruct<i8>;
a2 = GenericStruct::<_>(1, 456);

let b2: i32 = a2.0;
let c2: usize = a2.1;
}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust.test/fail_compilation/generics2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct GenericStruct<T>(T, usize);

fn main() {
let a2: GenericStruct<i8>;
a2 = GenericStruct(1, 456);

let b2: i32 = a2.0;
let c2: usize = a2.1;
}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust.test/fail_compilation/generics3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct GenericStruct<T>(T, usize);

fn main() {
let a2;
a2 = GenericStruct::<i8>(1, 456);

let b2: i32 = a2.0;
let c2: usize = a2.1;
}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust.test/fail_compilation/generics4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct GenericStruct<T>(T, usize);

fn main() {
let a2;
a2 = GenericStruct::<i8, i32>(1, 456);

let b2: i32 = a2.0;
let c2: usize = a2.1;
}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust.test/fail_compilation/generics5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct GenericStruct<T>(T, usize);

fn main() {
let a2;
a2 = GenericStruct::<i8, T>(1, 456);

let b2: i32 = a2.0;
let c2: usize = a2.1;
}

0 comments on commit 1c6eaa1

Please sign in to comment.