Skip to content

Commit e2ae5f2

Browse files
zygoloidjonmeow
andauthored
Remove the special case for i32. (#4543)
For the few remaining uses of the builtin `i32` type, manually build an `IntType(Signed, 32)` value instead. These are: - The return type of `Run`. - The type that int literals in an `if` expression are converted into. - The type of an array index expression. We should consider converting those three cases away from `i32` over time. --------- Co-authored-by: Jon Ross-Perkins <[email protected]>
1 parent b5368b3 commit e2ae5f2

File tree

348 files changed

+29330
-26511
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

348 files changed

+29330
-26511
lines changed

core/prelude/operators/as.carbon

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ interface ImplicitAs(Dest:! type) {
1919

2020
// TODO: These impls should live with Core.Int, but currently that's a builtin
2121
// not a class type, so there is no other library these can go in.
22-
impl IntLiteral() as ImplicitAs(i32) {
23-
fn Convert[self: Self]() -> i32 = "int.convert_checked";
24-
}
25-
2622
impl forall [N:! IntLiteral()] IntLiteral() as ImplicitAs(Int(N)) {
2723
fn Convert[self: Self]() -> Int(N) = "int.convert_checked";
2824
}
@@ -31,10 +27,6 @@ impl forall [N:! IntLiteral()] IntLiteral() as ImplicitAs(UInt(N)) {
3127
fn Convert[self: Self]() -> UInt(N) = "int.convert_checked";
3228
}
3329

34-
impl i32 as ImplicitAs(IntLiteral()) {
35-
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
36-
}
37-
3830
impl forall [N:! IntLiteral()] Int(N) as ImplicitAs(IntLiteral()) {
3931
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
4032
}
@@ -44,10 +36,6 @@ impl forall [N:! IntLiteral()] UInt(N) as ImplicitAs(IntLiteral()) {
4436
}
4537

4638
// TODO: Remove these once ImplicitAs extends As.
47-
impl IntLiteral() as As(i32) {
48-
fn Convert[self: Self]() -> i32 = "int.convert_checked";
49-
}
50-
5139
impl forall [N:! IntLiteral()] IntLiteral() as As(Int(N)) {
5240
fn Convert[self: Self]() -> Int(N) = "int.convert_checked";
5341
}
@@ -56,10 +44,6 @@ impl forall [N:! IntLiteral()] IntLiteral() as As(UInt(N)) {
5644
fn Convert[self: Self]() -> UInt(N) = "int.convert_checked";
5745
}
5846

59-
impl i32 as As(IntLiteral()) {
60-
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
61-
}
62-
6347
impl forall [N:! IntLiteral()] Int(N) as As(IntLiteral()) {
6448
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
6549
}

core/prelude/types.carbon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export import library "prelude/types/bool";
1212
// import library "prelude/types/i32";
1313

1414
fn IntLiteral() -> type = "int_literal.make_type";
15-
fn Int32() -> type = "int.make_type_32";
1615
fn Int(size: IntLiteral()) -> type = "int.make_type_signed";
1716
fn UInt(size: IntLiteral()) -> type = "int.make_type_unsigned";
1817
fn Float(size: IntLiteral()) -> type = "float.make_type";

core/prelude/types/i32.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package Core library "prelude/types/i32";
66

7-
// For Int32():
7+
// For Int():
88
import library "prelude/types";
99
import library "prelude/operators";
1010

toolchain/check/context.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,6 @@ class TypeCompleter {
10101010
case SemIR::BuiltinInstKind::Invalid:
10111011
case SemIR::BuiltinInstKind::BoolType:
10121012
case SemIR::BuiltinInstKind::IntLiteralType:
1013-
case SemIR::BuiltinInstKind::IntType:
10141013
case SemIR::BuiltinInstKind::FloatType:
10151014
case SemIR::BuiltinInstKind::NamespaceType:
10161015
case SemIR::BuiltinInstKind::BoundMethodType:
@@ -1368,6 +1367,17 @@ auto Context::GetGenericInterfaceType(SemIR::InterfaceId interface_id,
13681367
*this, interface_id, enclosing_specific_id);
13691368
}
13701369

1370+
auto Context::GetInt32Type() -> SemIR::TypeId {
1371+
auto bit_width_const_id = TryEvalInst(
1372+
*this, SemIR::InstId::Invalid,
1373+
SemIR::IntValue{
1374+
.type_id = GetBuiltinType(SemIR::BuiltinInstKind::IntLiteralType),
1375+
.int_id = ints().Add(32)});
1376+
return GetCompleteTypeImpl<SemIR::IntType>(
1377+
*this, SemIR::IntKind::Signed,
1378+
constant_values().GetInstId(bit_width_const_id));
1379+
}
1380+
13711381
auto Context::GetInterfaceType(SemIR::InterfaceId interface_id,
13721382
SemIR::SpecificId specific_id) -> SemIR::TypeId {
13731383
return GetTypeImpl<SemIR::FacetType>(

toolchain/check/context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ class Context {
406406
SemIR::SpecificId enclosing_specific_id)
407407
-> SemIR::TypeId;
408408

409+
// Returns the type `i32`.
410+
auto GetInt32Type() -> SemIR::TypeId;
411+
409412
// Gets the facet type corresponding to a particular interface.
410413
auto GetInterfaceType(SemIR::InterfaceId interface_id,
411414
SemIR::SpecificId specific_id) -> SemIR::TypeId;

toolchain/check/convert.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ static auto MakeElementAccessInst(Context& context, SemIR::LocId loc_id,
153153
// index so that we don't need an integer literal instruction here, and
154154
// remove this special case.
155155
auto index_id = block.template AddInst<SemIR::IntValue>(
156-
loc_id,
157-
{.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::IntType),
158-
.int_id = context.ints().AddUnsigned(llvm::APInt(32, i))});
156+
loc_id, {.type_id = context.GetBuiltinType(
157+
SemIR::BuiltinInstKind::IntLiteralType),
158+
.int_id = context.ints().Add(static_cast<int64_t>(i))});
159159
return block.template AddInst<AccessInstT>(
160160
loc_id, {elem_type_id, aggregate_id, index_id});
161161
} else {

toolchain/check/eval.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,6 @@ static auto MakeConstantForBuiltinCall(Context& context, SemIRLoc loc,
10571057
SemIR::InstId::BuiltinIntLiteralType);
10581058
}
10591059

1060-
case SemIR::BuiltinFunctionKind::IntMakeType32: {
1061-
return context.constant_values().Get(SemIR::InstId::BuiltinIntType);
1062-
}
1063-
10641060
case SemIR::BuiltinFunctionKind::IntMakeTypeSigned: {
10651061
return MakeIntTypeResult(context, loc, SemIR::IntKind::Signed, arg_ids[0],
10661062
phase);

toolchain/check/handle_function.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ static auto BuildFunctionDecl(Context& context,
300300
!function_info.param_patterns_id.is_valid() ||
301301
!context.inst_blocks().Get(function_info.param_patterns_id).empty() ||
302302
(return_type_id.is_valid() &&
303-
return_type_id !=
304-
context.GetBuiltinType(SemIR::BuiltinInstKind::IntType) &&
303+
return_type_id != context.GetInt32Type() &&
305304
return_type_id != context.GetTupleType({}))) {
306305
CARBON_DIAGNOSTIC(InvalidMainRunSignature, Error,
307306
"invalid signature for `Main.Run` function; expected "

toolchain/check/handle_if_expr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ static auto DecayIntLiteralToSizedInt(Context& context, Parse::NodeId node_id,
3838
-> SemIR::InstId {
3939
if (context.types().GetInstId(context.insts().Get(operand_id).type_id()) ==
4040
SemIR::InstId::BuiltinIntLiteralType) {
41-
operand_id = ConvertToValueOfType(
42-
context, node_id, operand_id,
43-
context.GetBuiltinType(SemIR::BuiltinInstKind::IntType));
41+
operand_id = ConvertToValueOfType(context, node_id, operand_id,
42+
context.GetInt32Type());
4443
}
4544
return operand_id;
4645
}

toolchain/check/handle_index.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
133133
case CARBON_KIND(SemIR::ArrayType array_type): {
134134
auto index_loc_id = context.insts().GetLocId(index_inst_id);
135135
auto cast_index_id = ConvertToValueOfType(
136-
context, index_loc_id, index_inst_id,
137-
context.GetBuiltinType(SemIR::BuiltinInstKind::IntType));
136+
context, index_loc_id, index_inst_id, context.GetInt32Type());
138137
auto array_cat =
139138
SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
140139
if (array_cat == SemIR::ExprCategory::Value) {

0 commit comments

Comments
 (0)