aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-02-08 20:45:26 -0500
committerAndrew Kelley <superjoe30@gmail.com>2018-02-08 20:45:26 -0500
commitf9be9703758607bf01ada328c59b5b6b4a83bb83 (patch)
treef6774eb6e844e21629d48e21c83dcd789fe8bb08 /src
parent57edd4dcb31eeaca69b93d2caf0e1f4eb3772e3e (diff)
parent1c236b0766bbc68f1b04e32a95683e273b26714c (diff)
downloadzig-f9be9703758607bf01ada328c59b5b6b4a83bb83.tar.gz
zig-f9be9703758607bf01ada328c59b5b6b4a83bb83.zip
Merge remote-tracking branch 'origin/master' into error-sets
Diffstat (limited to 'src')
-rw-r--r--src/analyze.cpp4
-rw-r--r--src/ir.cpp30
-rw-r--r--src/main.cpp33
3 files changed, 48 insertions, 19 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index a7adbf4a3a..b6f08b7aec 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -3309,6 +3309,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
is_const, init_val, &tld_var->base);
tld_var->var->linkage = linkage;
+ if (implicit_type != nullptr && type_is_invalid(implicit_type)) {
+ tld_var->var->value->type = g->builtin_types.entry_invalid;
+ }
+
if (var_decl->align_expr != nullptr) {
if (!analyze_const_align(g, tld_var->base.parent_scope, var_decl->align_expr, &tld_var->var->align_bytes)) {
tld_var->var->value->type = g->builtin_types.entry_invalid;
diff --git a/src/ir.cpp b/src/ir.cpp
index 728a3e815c..9cc05a2bc2 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -6408,6 +6408,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
if (other_type->id == TypeTableEntryIdFloat) {
return true;
} else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) {
+ if (!other_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
+ Buf *val_buf = buf_alloc();
+ bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
+ ir_add_error(ira, instruction,
+ buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
+ buf_ptr(val_buf),
+ buf_ptr(&other_type->name)));
+ return false;
+ }
if (bigint_fits_in_bits(&const_val->data.x_bigint, other_type->data.integral.bit_count,
other_type->data.integral.is_signed))
{
@@ -6420,6 +6429,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
if (const_val_fits_in_num_lit(const_val, child_type)) {
return true;
} else if (child_type->id == TypeTableEntryIdInt && const_val_is_int) {
+ if (!child_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
+ Buf *val_buf = buf_alloc();
+ bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
+ ir_add_error(ira, instruction,
+ buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
+ buf_ptr(val_buf),
+ buf_ptr(&child_type->name)));
+ return false;
+ }
if (bigint_fits_in_bits(&const_val->data.x_bigint,
child_type->data.integral.bit_count,
child_type->data.integral.is_signed))
@@ -6590,9 +6608,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry
}
// slice const
- if (expected_type->id == TypeTableEntryIdStruct && actual_type->id == TypeTableEntryIdStruct &&
- expected_type->data.structure.is_slice && actual_type->data.structure.is_slice)
- {
+ if (is_slice(expected_type) && is_slice(actual_type)) {
TypeTableEntry *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry;
TypeTableEntry *expected_ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry;
if ((!actual_ptr_type->data.pointer.is_const || expected_ptr_type->data.pointer.is_const) &&
@@ -6872,10 +6888,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
}
// implicit [N]T to []const T
- if (expected_type->id == TypeTableEntryIdStruct &&
- expected_type->data.structure.is_slice &&
- actual_type->id == TypeTableEntryIdArray)
- {
+ if (is_slice(expected_type) && actual_type->id == TypeTableEntryIdArray) {
TypeTableEntry *ptr_type = expected_type->data.structure.fields[slice_ptr_index].type_entry;
assert(ptr_type->id == TypeTableEntryIdPointer);
@@ -6887,8 +6900,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
}
// implicit &const [N]T to []const T
- if (expected_type->id == TypeTableEntryIdStruct &&
- expected_type->data.structure.is_slice &&
+ if (is_slice(expected_type) &&
actual_type->id == TypeTableEntryIdPointer &&
actual_type->data.pointer.is_const &&
actual_type->data.pointer.child_type->id == TypeTableEntryIdArray)
diff --git a/src/main.cpp b/src/main.cpp
index a533973de9..eab7f29b10 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -341,6 +341,7 @@ int main(int argc, char **argv) {
const char *zig_exe_path = arg0;
const char *build_file = "build.zig";
bool asked_for_help = false;
+ bool asked_to_init = false;
init_all_targets();
@@ -352,6 +353,9 @@ int main(int argc, char **argv) {
if (strcmp(argv[i], "--help") == 0) {
asked_for_help = true;
args.append(argv[i]);
+ } else if (strcmp(argv[i], "--init") == 0) {
+ asked_to_init = true;
+ args.append(argv[i]);
} else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) {
build_file = argv[i + 1];
i += 1;
@@ -416,6 +420,7 @@ int main(int argc, char **argv) {
"\n"
"General Options:\n"
" --help Print this help and exit\n"
+ " --init Generate a build.zig template\n"
" --build-file [file] Override path to build.zig\n"
" --cache-dir [path] Override path to cache directory\n"
" --verbose Print commands before executing them\n"
@@ -428,7 +433,6 @@ int main(int argc, char **argv) {
" --prefix [path] Override default install prefix\n"
"\n"
"Project-specific options become available when the build file is found.\n"
- "Run this command with no options to generate a build.zig template.\n"
"\n"
"Advanced Options:\n"
" --build-file [file] Override path to build.zig\n"
@@ -441,17 +445,26 @@ int main(int argc, char **argv) {
" --verbose-cimport Enable compiler debug output for C imports\n"
"\n"
, zig_exe_path);
- return 0;
- }
- Buf *build_template_path = buf_alloc();
- os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path);
+ return EXIT_SUCCESS;
+ } else if (asked_to_init) {
+ Buf *build_template_path = buf_alloc();
+ os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path);
- if ((err = os_copy_file(build_template_path, &build_file_abs))) {
- fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err));
- } else {
- fprintf(stderr, "Wrote build.zig template\n");
+ if ((err = os_copy_file(build_template_path, &build_file_abs))) {
+ fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err));
+ } else {
+ fprintf(stderr, "Wrote build.zig template\n");
+ }
+ return EXIT_SUCCESS;
}
- return 1;
+
+ fprintf(stderr,
+ "No 'build.zig' file found.\n"
+ "Initialize a 'build.zig' template file with `zig build --init`,\n"
+ "or build an executable directly with `zig build-exe $FILENAME.zig`.\n"
+ "See: `zig build --help` or `zig help` for more options.\n"
+ );
+ return EXIT_FAILURE;
}
PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),