Skip to content

Commit fbc94cd

Browse files
committed
Introduce the ! type (named "never")
1 parent cf203aa commit fbc94cd

12 files changed

+214
-30
lines changed

gcc/rust/backend/rust-compile-context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ class TyTyResolveCompile : public TyTy::TyVisitor
503503
translated = compiled_type;
504504
}
505505

506+
void visit (TyTy::NeverType &) override
507+
{
508+
translated = ctx->get_backend ()->void_type ();
509+
}
510+
506511
private:
507512
TyTyResolveCompile (Context *ctx) : ctx (ctx), translated (nullptr) {}
508513

gcc/rust/backend/rust-compile-tyty.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ class TyTyCompile : public TyTy::TyVisitor
222222
= backend->named_type ("str", raw_str, Linemap::predeclared_location ());
223223
}
224224

225+
void visit (TyTy::NeverType &) override
226+
{
227+
translated = backend->void_type ();
228+
}
229+
225230
private:
226231
TyTyCompile (::Backend *backend)
227232
: backend (backend), translated (nullptr),

gcc/rust/typecheck/rust-hir-type-check-expr.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class TypeCheckExpr : public TypeCheckBase
146146
{
147147
if (!expr.has_return_expr ())
148148
{
149-
infered = new TyTy::TupleType (expr.get_mappings ().get_hirid ());
149+
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
150150
return;
151151
}
152152

@@ -165,6 +165,8 @@ class TypeCheckExpr : public TypeCheckBase
165165
fn_return_tyty->append_reference (expr_ty->get_ref ());
166166
for (auto &ref : infered->get_combined_refs ())
167167
fn_return_tyty->append_reference (ref);
168+
169+
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
168170
}
169171

170172
void visit (HIR::CallExpr &expr) override
@@ -904,7 +906,7 @@ class TypeCheckExpr : public TypeCheckBase
904906
context->swap_head_loop_context (unified_ty);
905907
}
906908

907-
infered = new TyTy::TupleType (expr.get_mappings ().get_hirid ());
909+
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
908910
}
909911

910912
void visit (HIR::ContinueExpr &expr) override
@@ -916,7 +918,7 @@ class TypeCheckExpr : public TypeCheckBase
916918
return;
917919
}
918920

919-
infered = new TyTy::TupleType (expr.get_mappings ().get_hirid ());
921+
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
920922
}
921923

922924
void visit (HIR::BorrowExpr &expr) override

gcc/rust/typecheck/rust-hir-type-check-stmt.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class TypeCheckStmt : public TypeCheckBase
4242
void visit (HIR::ExprStmtWithBlock &stmt) override
4343
{
4444
infered = TypeCheckExpr::Resolve (stmt.get_expr (), inside_loop);
45+
46+
if (stmt.is_unit_check_needed ())
47+
{
48+
auto unit = new TyTy::TupleType (stmt.get_mappings ().get_hirid ());
49+
infered = unit->unify (infered);
50+
}
4551
}
4652

4753
void visit (HIR::ExprStmtWithoutBlock &stmt) override

gcc/rust/typecheck/rust-hir-type-check.cc

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,44 +82,27 @@ TypeResolution::Resolve (HIR::Crate &crate)
8282
void
8383
TypeCheckExpr::visit (HIR::BlockExpr &expr)
8484
{
85-
TyTy::BaseType *block_tyty
86-
= new TyTy::TupleType (expr.get_mappings ().get_hirid ());
87-
8885
expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool {
89-
bool is_final_stmt = expr.is_final_stmt (s);
90-
bool has_final_expr = expr.has_expr () && expr.tail_expr_reachable ();
91-
bool stmt_is_final_expr = is_final_stmt && !has_final_expr;
92-
9386
auto resolved = TypeCheckStmt::Resolve (s, inside_loop);
9487
if (resolved == nullptr)
9588
{
9689
rust_error_at (s->get_locus_slow (), "failure to resolve type");
9790
return false;
9891
}
9992

100-
if (stmt_is_final_expr)
101-
{
102-
delete block_tyty;
103-
block_tyty = resolved;
104-
}
105-
else if (!resolved->is_unit ())
106-
{
107-
rust_error_at (s->get_locus_slow (), "expected () got %s",
108-
resolved->as_string ().c_str ());
109-
}
110-
11193
return true;
11294
});
11395

11496
if (expr.has_expr ())
115-
{
116-
delete block_tyty;
117-
118-
block_tyty
119-
= TypeCheckExpr::Resolve (expr.get_final_expr ().get (), inside_loop);
120-
}
121-
122-
infered = block_tyty->clone ();
97+
infered = TypeCheckExpr::Resolve (
98+
expr.get_final_expr ().get (),
99+
inside_loop)->clone ();
100+
else if (expr.is_tail_reachable ())
101+
infered = new TyTy::TupleType (
102+
expr.get_mappings ().get_hirid ());
103+
else
104+
infered = new TyTy::NeverType (
105+
expr.get_mappings ().get_hirid ());
123106
}
124107

125108
// RUST_HIR_TYPE_CHECK_STRUCT_FIELD

gcc/rust/typecheck/rust-substitution-mapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class SubstMapper : public TyTy::TyVisitor
9494
void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
9595
void visit (TyTy::ParamType &) override { gcc_unreachable (); }
9696
void visit (TyTy::StrType &) override { gcc_unreachable (); }
97+
void visit (TyTy::NeverType &) override { gcc_unreachable (); }
9798

9899
private:
99100
SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus)
@@ -152,6 +153,7 @@ class SubstMapperInternal : public TyTy::TyVisitor
152153
void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
153154
void visit (TyTy::ParamType &) override { gcc_unreachable (); }
154155
void visit (TyTy::StrType &) override { gcc_unreachable (); }
156+
void visit (TyTy::NeverType &) override { gcc_unreachable (); }
155157

156158
private:
157159
SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings)
@@ -206,6 +208,7 @@ class SubstMapperFromExisting : public TyTy::TyVisitor
206208
void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
207209
void visit (TyTy::ParamType &) override { gcc_unreachable (); }
208210
void visit (TyTy::StrType &) override { gcc_unreachable (); }
211+
void visit (TyTy::NeverType &) override { gcc_unreachable (); }
209212

210213
private:
211214
SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver)

gcc/rust/typecheck/rust-tycheck-dump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class TypeResolverDump : public TypeCheckBase
107107
return true;
108108
});
109109

110-
if (expr.has_expr () && expr.tail_expr_reachable ())
110+
if (expr.has_expr ())
111111
{
112112
dump += indent ();
113113
expr.expr->accept_vis (*this);

gcc/rust/typecheck/rust-tyty-call.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TypeCheckCallExpr : private TyVisitor
5353
void visit (ReferenceType &type) override { gcc_unreachable (); }
5454
void visit (ParamType &) override { gcc_unreachable (); }
5555
void visit (StrType &) override { gcc_unreachable (); }
56+
void visit (NeverType &) override { gcc_unreachable (); }
5657

5758
// tuple-structs
5859
void visit (ADTType &type) override;
@@ -100,6 +101,7 @@ class TypeCheckMethodCallExpr : private TyVisitor
100101
void visit (ReferenceType &type) override { gcc_unreachable (); }
101102
void visit (ParamType &) override { gcc_unreachable (); }
102103
void visit (StrType &) override { gcc_unreachable (); }
104+
void visit (NeverType &) override { gcc_unreachable (); }
103105

104106
// FIXME
105107
void visit (FnPtr &type) override { gcc_unreachable (); }

0 commit comments

Comments
 (0)