Skip to content

Commit 966e522

Browse files
authored
Merge pull request #21 from philberty/phil/compilation-simple
WIP type infer error handling in LetStmt
2 parents 0f348c7 + caf7d21 commit 966e522

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

gcc/rust/analysis/rust-resolution.cc

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#include "rust-resolution.h"
2+
#include "rust-diagnostics.h"
23

34
namespace Rust {
45
namespace Analysis {
56

6-
TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate) {}
7+
TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate)
8+
{
9+
// push all builtin types
10+
// base is parse_path_ident_segment based up on segments
11+
/* scope.Insert ("u8",
12+
new AST::MaybeNamedParam (Identifier ("u8"),
13+
AST::MaybeNamedParam::IDENTIFIER,
14+
NULL, Location ()));*/
15+
}
716

817
TypeResolution::~TypeResolution () {}
918

@@ -40,7 +49,9 @@ TypeResolution::visit (AST::AttrInputMetaItemContainer &input)
4049

4150
void
4251
TypeResolution::visit (AST::IdentifierExpr &ident_expr)
43-
{}
52+
{
53+
printf ("IdentifierExpr %s\n", ident_expr.as_string ().c_str ());
54+
}
4455

4556
void
4657
TypeResolution::visit (AST::Lifetime &lifetime)
@@ -80,7 +91,11 @@ TypeResolution::visit (AST::QualifiedPathInType &path)
8091
// rust-expr.h
8192
void
8293
TypeResolution::visit (AST::LiteralExpr &expr)
83-
{}
94+
{
95+
printf ("LiteralExpr: %s\n", expr.as_string ().c_str ());
96+
// figure out what this type is and push it onto the
97+
}
98+
8499
void
85100
TypeResolution::visit (AST::AttrInputLiteral &attr_input)
86101
{}
@@ -526,44 +541,70 @@ TypeResolution::visit (AST::SlicePattern &pattern)
526541
void
527542
TypeResolution::visit (AST::EmptyStmt &stmt)
528543
{}
529-
void
530544

545+
void
531546
TypeResolution::visit (AST::LetStmt &stmt)
532547
{
533-
printf ("Within LetStmt: %s\n", stmt.as_string ().c_str ());
534-
535-
if (!stmt.has_type ())
548+
AST::Type *inferedType = NULL;
549+
if (stmt.has_type ())
550+
{
551+
inferedType = stmt.type.get ();
552+
}
553+
else if (stmt.has_init_expr ())
536554
{
537-
// infer the type
538-
printf ("XXX UNKNOWN TYPE PLEASE INFER ME\n");
555+
stmt.init_expr->accept_vis (*this);
556+
557+
if (typeBuffer.empty ())
558+
{
559+
rust_error_at (
560+
stmt.init_expr->get_locus_slow (),
561+
"unable to determine type for declaration from init expr");
562+
return;
563+
}
564+
565+
inferedType = typeBuffer.back ();
566+
typeBuffer.pop_back ();
567+
}
568+
else
569+
{
570+
rust_error_at (stmt.locus, "unable to determine type for declaration");
571+
return;
539572
}
540573

541574
// TODO check we know what the type is
542575

576+
// get all the names part of this declaration and add the types to the scope
543577
stmt.variables_pattern->accept_vis (*this);
544-
545578
for (auto it = letPatternBuffer.begin (); it != letPatternBuffer.end (); it++)
546579
{
547-
scope.Insert (it->variable_ident, stmt.type.get ());
580+
scope.Insert (it->variable_ident, inferedType);
548581
}
549-
550582
letPatternBuffer.clear ();
551583
}
552584

553585
void
554586
TypeResolution::visit (AST::ExprStmtWithoutBlock &stmt)
555-
{}
587+
{
588+
printf ("ExprStmtWithoutBlock: %s\n", stmt.as_string ().c_str ());
589+
stmt.expr->accept_vis (*this);
590+
}
591+
556592
void
557593
TypeResolution::visit (AST::ExprStmtWithBlock &stmt)
558-
{}
594+
{
595+
printf ("ExprStmtWithBlock: %s\n", stmt.as_string ().c_str ());
596+
stmt.expr->accept_vis (*this);
597+
}
559598

560599
// rust-type.h
561600
void
562601
TypeResolution::visit (AST::TraitBound &bound)
563602
{}
603+
564604
void
565605
TypeResolution::visit (AST::ImplTraitType &type)
566606
{}
607+
567608
void
568609
TypeResolution::visit (AST::TraitObjectType &type)
569610
{}

gcc/rust/analysis/rust-resolution.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ class TypeResolution : public AST::ASTVisitor
229229
AST::Crate &crate;
230230

231231
std::vector<AST::IdentifierPattern> letPatternBuffer;
232+
std::vector<AST::Type *> typeBuffer;
232233
};
233234

234235
} // namespace Analysis

gcc/rust/ast/rust-stmt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ class ExprStmt : public Stmt
147147
* difficulties, can only be guaranteed to hold an expression). */
148148
class ExprStmtWithoutBlock : public ExprStmt
149149
{
150+
public:
150151
// ExprWithoutBlock* expr;
151152
/* HACK: cannot ensure type safety of ExprWithoutBlock due to Pratt parsing,
152153
* so have to store more general type of Expr. FIXME: fix this issue somehow
153154
* or redesign AST. */
154155
//::std::unique_ptr<ExprWithoutBlock> expr;
155156
::std::unique_ptr<Expr> expr;
156157

157-
public:
158158
/*~ExpressionStatementWithoutBlock() {
159159
delete expr;
160160
}*/
@@ -201,10 +201,10 @@ class ExprStmtWithoutBlock : public ExprStmt
201201
// Statement containing an expression with a block
202202
class ExprStmtWithBlock : public ExprStmt
203203
{
204+
public:
204205
// ExprWithBlock* expr;
205206
::std::unique_ptr<ExprWithBlock> expr;
206207

207-
public:
208208
/*~ExpressionStatementWithBlock() {
209209
delete expr;
210210
}*/

0 commit comments

Comments
 (0)