aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xci/azure/macos_arm64_script120
-rwxr-xr-xci/azure/macos_script19
-rw-r--r--lib/std/zig/parse.zig2
-rw-r--r--src/stage1/all_types.hpp1
-rw-r--r--src/stage1/analyze.cpp13
-rw-r--r--src/stage1/ast_render.cpp16
-rw-r--r--src/stage1/ir.cpp83
-rw-r--r--src/stage1/parser.cpp25
-rw-r--r--src/translate_c.zig8
-rw-r--r--test/run_translated_c.zig14
10 files changed, 145 insertions, 156 deletions
diff --git a/ci/azure/macos_arm64_script b/ci/azure/macos_arm64_script
index 54d0380fa4..c1b93012db 100755
--- a/ci/azure/macos_arm64_script
+++ b/ci/azure/macos_arm64_script
@@ -3,24 +3,31 @@
set -x
set -e
-brew update && brew install s3cmd ninja gnu-tar
+brew update && brew install s3cmd
ZIGDIR="$(pwd)"
+
+HOST_ARCH="x86_64"
+HOST_TARGET="$HOST_ARCH-macos-gnu"
+HOST_MCPU="baseline"
+HOST_CACHE_BASENAME="zig+llvm+lld+clang-$HOST_TARGET-0.8.0-dev.2168+2d1196773"
+HOST_PREFIX="$HOME/$HOST_CACHE_BASENAME"
+
ARCH="aarch64"
-# {product}-{os}{sdk_version}-{arch}-{llvm_version}-{cmake_build_type}
-CACHE_HOST_BASENAME="ci-llvm-macos10.15-x86_64-12.0.0.1-release"
-CACHE_ARM64_BASENAME="ci-llvm-macos11.0-arm64-12.0.0.1-release"
-PREFIX_HOST="$HOME/$CACHE_HOST_BASENAME"
-PREFIX_ARM64="$HOME/$CACHE_ARM64_BASENAME"
+TARGET="$ARCH-macos-gnu"
+MCPU="cyclone"
+CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.8.0-dev.2168+2d1196773"
+PREFIX="$HOME/$CACHE_BASENAME"
+
JOBS="-j2"
-rm -rf $PREFIX
+rm -rf $HOST_PREFIX $PREFIX
cd $HOME
-wget -nv "https://ziglang.org/deps/$CACHE_HOST_BASENAME.tar.xz"
-wget -nv "https://ziglang.org/deps/$CACHE_ARM64_BASENAME.tar.xz"
-gtar xf "$CACHE_HOST_BASENAME.tar.xz"
-gtar xf "$CACHE_ARM64_BASENAME.tar.xz"
+wget -nv "https://ziglang.org/deps/$HOST_CACHE_BASENAME.tar.xz"
+wget -nv "https://ziglang.org/deps/$CACHE_BASENAME.tar.xz"
+tar xf "$HOST_CACHE_BASENAME.tar.xz"
+tar xf "$CACHE_BASENAME.tar.xz"
cd $ZIGDIR
@@ -30,83 +37,75 @@ git config core.abbrev 9
git fetch --unshallow || true
git fetch --tags
-# Select xcode: latest version found on vmImage macOS-10.15 .
-DEVELOPER_DIR=/Applications/Xcode_12.4.app
+# Build host zig compiler in debug so that we can get the
+# current version when packaging
-export ZIG_LOCAL_CACHE_DIR="$ZIGDIR/zig-cache"
-export ZIG_GLOBAL_CACHE_DIR="$ZIGDIR/zig-cache"
+ZIG="$HOST_PREFIX/bin/zig"
-# Build zig for host and use `Debug` type to make builds a little faster.
+export CC="$ZIG cc -target $HOST_TARGET -mcpu=$HOST_MCPU"
+export CXX="$ZIG c++ -target $HOST_TARGET -mcpu=$HOST_MCPU"
-cd $ZIGDIR
mkdir build.host
cd build.host
-cmake -G "Ninja" .. \
+cmake .. \
-DCMAKE_INSTALL_PREFIX="$(pwd)/release" \
- -DCMAKE_PREFIX_PATH="$PREFIX_HOST" \
- -DCMAKE_BUILD_TYPE="Debug" \
- -DZIG_STATIC="OFF"
-
-# Build but do not install.
-ninja $JOBS
+ -DCMAKE_PREFIX_PATH="$HOST_PREFIX" \
+ -DCMAKE_BUILD_TYPE=Debug \
+ -DZIG_TARGET_TRIPLE="$HOST_TARGET" \
+ -DZIG_TARGET_MCPU="$HOST_MCPU" \
+ -DZIG_STATIC=ON
-ZIG_EXE="$ZIGDIR/build.host/zig"
+make $JOBS install
-# Build zig for arm64 target.
-# - use `Release` type for published tarballs
-# - ad-hoc codesign with linker
-# - note: apple quarantine of downloads (eg. via safari) still apply
+unset CC
+unset CXX
+# Build zig compiler cross-compiled for arm64
cd $ZIGDIR
-mkdir build.arm64
-cd build.arm64
-cmake -G "Ninja" .. \
+
+ZIG="$ZIGDIR/build.host/release/bin/zig"
+
+export CC="$ZIG cc -target $TARGET -mcpu=$MCPU"
+export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU"
+
+mkdir build
+cd build
+cmake .. \
-DCMAKE_INSTALL_PREFIX="$(pwd)/release" \
- -DCMAKE_PREFIX_PATH="$PREFIX_ARM64" \
- -DCMAKE_BUILD_TYPE="Release" \
- -DCMAKE_CROSSCOMPILING="True" \
- -DCMAKE_SYSTEM_NAME="Darwin" \
- -DCMAKE_C_FLAGS="-arch arm64" \
- -DCMAKE_CXX_FLAGS="-arch arm64" \
- -DCMAKE_EXE_LINKER_FLAGS="-lz -Xlinker -adhoc_codesign" \
- -DZIG_USE_LLVM_CONFIG="OFF" \
- -DZIG_EXECUTABLE="$ZIG_EXE" \
- -DZIG_TARGET_TRIPLE="${ARCH}-macos" \
- -DZIG_STATIC="OFF"
-
-ninja $JOBS install
-
-# Disable test because binary is foreign arch.
-#release/bin/zig build test
+ -DCMAKE_PREFIX_PATH="$PREFIX" \
+ -DCMAKE_BUILD_TYPE=Release \
+ -DZIG_TARGET_TRIPLE="$TARGET" \
+ -DZIG_TARGET_MCPU="$MCPU" \
+ -DZIG_EXECUTABLE="$ZIG" \
+ -DZIG_STATIC=ON
+
+make $JOBS install
+
+unset CC
+unset CXX
if [ "${BUILD_REASON}" != "PullRequest" ]; then
mv ../LICENSE release/
# We do not run test suite but still need langref.
mkdir -p release/docs
- $ZIG_EXE run ../doc/docgen.zig -- $ZIG_EXE ../doc/langref.html.in release/docs/langref.html
+ $ZIG run ../doc/docgen.zig -- $ZIG ../doc/langref.html.in release/docs/langref.html
# Produce the experimental std lib documentation.
mkdir -p release/docs/std
- $ZIG_EXE test ../lib/std/std.zig \
+ $ZIG test ../lib/std/std.zig \
--override-lib-dir ../lib \
-femit-docs=release/docs/std \
-fno-emit-bin
- # Remove the unnecessary bin dir in $prefix/bin/zig
mv release/bin/zig release/
rmdir release/bin
- # Remove the unnecessary zig dir in $prefix/lib/zig/std/std.zig
- mv release/lib/zig release/lib2
- rmdir release/lib
- mv release/lib2 release/lib
-
- VERSION=$($ZIG_EXE version)
+ VERSION=$(../build.host/release/bin/zig version)
DIRNAME="zig-macos-$ARCH-$VERSION"
TARBALL="$DIRNAME.tar.xz"
- gtar cJf "$TARBALL" release/ --owner=root --sort=name --transform="s,^release,${DIRNAME},"
- ln "$TARBALL" "$BUILD_ARTIFACTSTAGINGDIRECTORY/."
+ mv release "$DIRNAME"
+ tar cfJ "$TARBALL" "$DIRNAME"
mv "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg"
s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$TARBALL" s3://ziglang.org/builds/
@@ -114,12 +113,13 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then
SHASUM=$(shasum -a 256 $TARBALL | cut '-d ' -f1)
BYTESIZE=$(wc -c < $TARBALL)
- JSONFILE="tarball.json"
+ JSONFILE="macos-$GITBRANCH.json"
touch $JSONFILE
echo "{\"tarball\": \"$TARBALL\"," >>$JSONFILE
echo "\"shasum\": \"$SHASUM\"," >>$JSONFILE
echo "\"size\": \"$BYTESIZE\"}" >>$JSONFILE
+ s3cmd put -P --add-header="Cache-Control: max-age=0, must-revalidate" "$JSONFILE" "s3://ziglang.org/builds/$JSONFILE"
s3cmd put -P "$JSONFILE" "s3://ziglang.org/builds/$ARCH-macos-$VERSION.json"
# `set -x` causes these variables to be mangled.
diff --git a/ci/azure/macos_script b/ci/azure/macos_script
index d6d32612cc..238029e37e 100755
--- a/ci/azure/macos_script
+++ b/ci/azure/macos_script
@@ -7,21 +7,21 @@ brew update && brew install s3cmd
ZIGDIR="$(pwd)"
ARCH="x86_64"
-CACHE_BASENAME="zig+llvm+lld+clang-$ARCH-macos-gnu-0.8.0-dev.1939+5a3ea9bec"
+TARGET="$ARCH-macos-gnu"
+MCPU="baseline"
+CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.8.0-dev.2168+2d1196773"
PREFIX="$HOME/$CACHE_BASENAME"
JOBS="-j2"
rm -rf $PREFIX
cd $HOME
+
wget -nv "https://ziglang.org/deps/$CACHE_BASENAME.tar.xz"
tar xf "$CACHE_BASENAME.tar.xz"
ZIG="$PREFIX/bin/zig"
-NATIVE_LIBC_TXT="$HOME/native_libc.txt"
-$ZIG libc >"$NATIVE_LIBC_TXT"
-export ZIG_LIBC="$NATIVE_LIBC_TXT"
-export CC="$ZIG cc"
-export CXX="$ZIG c++"
+export CC="$ZIG cc -target $TARGET -mcpu=$MCPU"
+export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU"
cd $ZIGDIR
@@ -37,22 +37,21 @@ cmake .. \
-DCMAKE_INSTALL_PREFIX="$(pwd)/release" \
-DCMAKE_PREFIX_PATH="$PREFIX" \
-DCMAKE_BUILD_TYPE=Release \
- -DZIG_TARGET_TRIPLE="$ARCH-native-gnu" \
- -DZIG_TARGET_MCPU="baseline" \
+ -DZIG_TARGET_TRIPLE="$TARGET" \
+ -DZIG_TARGET_MCPU="$MCPU" \
-DZIG_STATIC=ON
# Now cmake will use zig as the C/C++ compiler. We reset the environment variables
# so that installation and testing do not get affected by them.
unset CC
unset CXX
-unset ZIG_LIBC
make $JOBS install
# Here we rebuild zig but this time using the Zig binary we just now produced to
# build zig1.o rather than relying on the one built with stage0. See
# https://github.com/ziglang/zig/issues/6830 for more details.
-cmake .. -DZIG_EXECUTABLE="$(pwd)/release/bin/zig" -DZIG_TARGET_MCPU="x86_64_v2"
+cmake .. -DZIG_EXECUTABLE="$(pwd)/release/bin/zig"
make $JOBS install
for step in test-toolchain test-std docs; do
diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig
index d6880ad273..b0da57f0e2 100644
--- a/lib/std/zig/parse.zig
+++ b/lib/std/zig/parse.zig
@@ -639,7 +639,7 @@ const Parser = struct {
};
}
- /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? (Keyword_anytype / TypeExpr)
+ /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr
fn parseFnProto(p: *Parser) !Node.Index {
const fn_token = p.eatToken(.keyword_fn) orelse return null_node;
diff --git a/src/stage1/all_types.hpp b/src/stage1/all_types.hpp
index 0991faf815..9eed42f4cd 100644
--- a/src/stage1/all_types.hpp
+++ b/src/stage1/all_types.hpp
@@ -718,7 +718,6 @@ struct AstNodeFnProto {
Buf *name;
ZigList<AstNode *> params;
AstNode *return_type;
- Token *return_anytype_token;
AstNode *fn_def_node;
// populated if this is an extern declaration
Buf *lib_name;
diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp
index 8ca845d2cf..a87fd80613 100644
--- a/src/stage1/analyze.cpp
+++ b/src/stage1/analyze.cpp
@@ -2125,18 +2125,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
return g->builtin_types.entry_invalid;
}
- if (fn_proto->return_anytype_token != nullptr) {
- if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
- add_node_error(g, fn_proto->return_type,
- buf_sprintf("return type 'anytype' not allowed in function with calling convention '%s'",
- calling_convention_name(fn_type_id.cc)));
- return g->builtin_types.entry_invalid;
- }
- add_node_error(g, proto_node,
- buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
- return g->builtin_types.entry_invalid;
- }
-
ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
if (type_is_invalid(specified_return_type)) {
fn_type_id.return_type = g->builtin_types.entry_invalid;
@@ -10220,4 +10208,3 @@ const char *float_op_to_name(BuiltinFnId op) {
zig_unreachable();
}
}
-
diff --git a/src/stage1/ast_render.cpp b/src/stage1/ast_render.cpp
index 9aebac1d28..ed53cf7ccb 100644
--- a/src/stage1/ast_render.cpp
+++ b/src/stage1/ast_render.cpp
@@ -490,17 +490,13 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, ")");
}
- if (node->data.fn_proto.return_anytype_token != nullptr) {
- fprintf(ar->f, "anytype");
- } else {
- AstNode *return_type_node = node->data.fn_proto.return_type;
- assert(return_type_node != nullptr);
- fprintf(ar->f, " ");
- if (node->data.fn_proto.auto_err_set) {
- fprintf(ar->f, "!");
- }
- render_node_grouped(ar, return_type_node);
+ AstNode *return_type_node = node->data.fn_proto.return_type;
+ assert(return_type_node != nullptr);
+ fprintf(ar->f, " ");
+ if (node->data.fn_proto.auto_err_set) {
+ fprintf(ar->f, "!");
}
+ render_node_grouped(ar, return_type_node);
break;
}
case NodeTypeFnDef:
diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp
index 2f345a8411..14192000ba 100644
--- a/src/stage1/ir.cpp
+++ b/src/stage1/ir.cpp
@@ -10104,19 +10104,12 @@ static IrInstSrc *ir_gen_fn_proto(IrBuilderSrc *irb, Scope *parent_scope, AstNod
}
IrInstSrc *return_type;
- if (node->data.fn_proto.return_anytype_token == nullptr) {
- if (node->data.fn_proto.return_type == nullptr) {
- return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
- } else {
- return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
- if (return_type == irb->codegen->invalid_inst_src)
- return irb->codegen->invalid_inst_src;
- }
+ if (node->data.fn_proto.return_type == nullptr) {
+ return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
} else {
- add_node_error(irb->codegen, node,
- buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
- return irb->codegen->invalid_inst_src;
- //return_type = nullptr;
+ return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
+ if (return_type == irb->codegen->invalid_inst_src)
+ return irb->codegen->invalid_inst_src;
}
return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, callconv_value, return_type, is_var_args);
@@ -14978,7 +14971,7 @@ static IrInstGen *ir_analyze_struct_literal_to_array(IrAnalyze *ira, IrInst* sou
if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;
-
+
size_t array_len = wanted_type->data.array.len;
size_t instr_field_count = actual_type->data.structure.src_field_count;
assert(array_len == instr_field_count);
@@ -20953,44 +20946,42 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
inst_fn_type_id.alignment = align_bytes;
}
- if (fn_proto_node->data.fn_proto.return_anytype_token == nullptr) {
- AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
- ZigType *specified_return_type = ir_analyze_type_expr(ira, impl_fn->child_scope, return_type_node);
- if (type_is_invalid(specified_return_type))
- return ira->codegen->invalid_inst_gen;
-
- if(!is_valid_return_type(specified_return_type)){
- ErrorMsg *msg = ir_add_error(ira, source_instr,
- buf_sprintf("call to generic function with %s return type '%s' not allowed", type_id_name(specified_return_type->id), buf_ptr(&specified_return_type->name)));
- add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("function declared here"));
+ AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
+ ZigType *specified_return_type = ir_analyze_type_expr(ira, impl_fn->child_scope, return_type_node);
+ if (type_is_invalid(specified_return_type))
+ return ira->codegen->invalid_inst_gen;
- Tld *tld = find_decl(ira->codegen, &fn_entry->fndef_scope->base, &specified_return_type->name);
- if (tld != nullptr) {
- add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("type declared here"));
- }
- return ira->codegen->invalid_inst_gen;
- }
+ if(!is_valid_return_type(specified_return_type)){
+ ErrorMsg *msg = ir_add_error(ira, source_instr,
+ buf_sprintf("call to generic function with %s return type '%s' not allowed", type_id_name(specified_return_type->id), buf_ptr(&specified_return_type->name)));
+ add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("function declared here"));
- if (fn_proto_node->data.fn_proto.auto_err_set) {
- ZigType *inferred_err_set_type = get_auto_err_set_type(ira->codegen, impl_fn);
- if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusSizeKnown)))
- return ira->codegen->invalid_inst_gen;
- inst_fn_type_id.return_type = get_error_union_type(ira->codegen, inferred_err_set_type, specified_return_type);
- } else {
- inst_fn_type_id.return_type = specified_return_type;
+ Tld *tld = find_decl(ira->codegen, &fn_entry->fndef_scope->base, &specified_return_type->name);
+ if (tld != nullptr) {
+ add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("type declared here"));
}
+ return ira->codegen->invalid_inst_gen;
+ }
- switch (type_requires_comptime(ira->codegen, specified_return_type)) {
- case ReqCompTimeYes:
- // Throw out our work and call the function as if it were comptime.
- return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,
- first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
- args_ptr, args_len, ret_ptr, call_result_loc);
- case ReqCompTimeInvalid:
+ if (fn_proto_node->data.fn_proto.auto_err_set) {
+ ZigType *inferred_err_set_type = get_auto_err_set_type(ira->codegen, impl_fn);
+ if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;
- case ReqCompTimeNo:
- break;
- }
+ inst_fn_type_id.return_type = get_error_union_type(ira->codegen, inferred_err_set_type, specified_return_type);
+ } else {
+ inst_fn_type_id.return_type = specified_return_type;
+ }
+
+ switch (type_requires_comptime(ira->codegen, specified_return_type)) {
+ case ReqCompTimeYes:
+ // Throw out our work and call the function as if it were comptime.
+ return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,
+ first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
+ args_ptr, args_len, ret_ptr, call_result_loc);
+ case ReqCompTimeInvalid:
+ return ira->codegen->invalid_inst_gen;
+ case ReqCompTimeNo:
+ break;
}
auto existing_entry = ira->codegen->generic_table.put_unique(generic_id, impl_fn);
diff --git a/src/stage1/parser.cpp b/src/stage1/parser.cpp
index f152f245b7..08323d3086 100644
--- a/src/stage1/parser.cpp
+++ b/src/stage1/parser.cpp
@@ -820,21 +820,19 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) {
AstNode *align_expr = ast_parse_byte_align(pc);
AstNode *section_expr = ast_parse_link_section(pc);
AstNode *callconv_expr = ast_parse_callconv(pc);
- Token *anytype = eat_token_if(pc, TokenIdKeywordAnyType);
Token *exmark = nullptr;
AstNode *return_type = nullptr;
- if (anytype == nullptr) {
- exmark = eat_token_if(pc, TokenIdBang);
- return_type = ast_parse_type_expr(pc);
- if (return_type == nullptr) {
- Token *next = peek_token(pc);
- ast_error(
- pc,
- next,
- "expected return type (use 'void' to return nothing), found: '%s'",
- token_name(next->id)
- );
- }
+
+ exmark = eat_token_if(pc, TokenIdBang);
+ return_type = ast_parse_type_expr(pc);
+ if (return_type == nullptr) {
+ Token *next = peek_token(pc);
+ ast_error(
+ pc,
+ next,
+ "expected return type (use 'void' to return nothing), found: '%s'",
+ token_name(next->id)
+ );
}
AstNode *res = ast_create_node(pc, NodeTypeFnProto, first);
@@ -844,7 +842,6 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) {
res->data.fn_proto.align_expr = align_expr;
res->data.fn_proto.section_expr = section_expr;
res->data.fn_proto.callconv_expr = callconv_expr;
- res->data.fn_proto.return_anytype_token = anytype;
res->data.fn_proto.auto_err_set = exmark != nullptr;
res->data.fn_proto.return_type = return_type;
diff --git a/src/translate_c.zig b/src/translate_c.zig
index 8244d66e94..1f8d818f0b 100644
--- a/src/translate_c.zig
+++ b/src/translate_c.zig
@@ -447,7 +447,13 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void {
// TODO https://github.com/ziglang/zig/issues/3756
// TODO https://github.com/ziglang/zig/issues/1802
const name = if (isZigPrimitiveType(decl_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ decl_name, c.getMangle() }) else decl_name;
- try c.unnamed_typedefs.putNoClobber(c.gpa, addr, name);
+ const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr);
+ if (result.found_existing) {
+ // One typedef can declare multiple names.
+ // Don't put this one in `decl_table` so it's processed later.
+ return;
+ }
+ result.entry.value = name;
// Put this typedef in the decl_table to avoid redefinitions.
try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name);
}
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index 314ef2889d..250f7e67bc 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -1476,4 +1476,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
+
+ cases.add("typedef with multiple names",
+ \\#include <stdlib.h>
+ \\typedef struct {
+ \\ char field;
+ \\} a_t, b_t;
+ \\
+ \\int main(void) {
+ \\ a_t a = { .field = 42 };
+ \\ b_t b = a;
+ \\ if (b.field != 42) abort();
+ \\ return 0;
+ \\}
+ , "");
}