Commit graph

175 commits

Author SHA1 Message Date
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
antlilja
b463e429b8 Remove len parameter from splat in standard lib 2023-07-12 15:35:57 -07:00
Robin Voetter
aa398034eb
testing: disable printing for targets that do not support it
The SPIR-V cannot print the helpful error messages from the
std.testing module. This commit overrides the default print
function used here to one that only prints if the target supports
it. For now, its only done for SPIR-V, but this function could
be adapted to more targets that need it.
2023-07-01 14:29:11 +02:00
Motiejus Jakštys
d41111d7ef mem: rename align*Generic to mem.align*
Anecdote 1: The generic version is way more popular than the non-generic
one in Zig codebase:

     git grep -w alignForward | wc -l
    56
     git grep -w alignForwardGeneric | wc -l
    149

     git grep -w alignBackward | wc -l
    6
     git grep -w alignBackwardGeneric | wc -l
    15

Anecdote 2: In my project (turbonss) that does much arithmetic and
alignment I exclusively use the Generic functions.

Anecdote 3: we used only the Generic versions in the Macho Man's linker
workshop.
2023-06-17 12:49:13 -07:00
Eric Joldasov
d04a262a3d
std.math: hard deprecate obsolete constants (soft deprecated in 0.10)
Followup to 5b8ac9821d.
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-13 23:45:12 +06:00
mlugg
4976b58ab1
Prevent analysis of functions only referenced at comptime
The idea here is that there are two ways we can reference a function at runtime:

* Through a direct call, i.e. where the function is comptime-known
* Through a function pointer

This means we can easily perform a form of rudimentary escape analysis
on functions. If we ever see a `decl_ref` or `ref` of a function, we
have a function pointer, which could "leak" into runtime code, so we
emit the function; but for a plain `decl_val`, there's no need to.

This change means that `comptime { _ = f; }` no longer forces a function
to be emitted, which was used for some things (mainly tests). These use
sites have been replaced with `_ = &f;`, which still triggers analysis
of the function body, since you're taking a pointer to the function.

Resolves: #6256
Resolves: #15353
2023-05-29 23:06:08 +01:00
Linus Groh
0f6fa3f20b std: Move std.debug.{TTY.Config,detectTTYConfig} to std.io.tty
Also get rid of the TTY wrapper struct, which was exlusively used as a
namespace - this is done by the tty.zig root struct now.

detectTTYConfig has been renamed to just detectConfig, which is enough
given the new namespace. Additionally, a doc comment had been added.
2023-05-24 10:15:02 +01:00
Linus Groh
39c2eee285 std.debug: Rename TTY.Color enum values to snake case 2023-05-24 10:15:02 +01:00
dweiller
bd3360e03d convert s[start..start+len] to s[start..][0..len] 2023-05-07 15:55:21 +10:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Jiacai Liu
d8c3c11c6c
std: add expectEqualDeep (#13995) 2023-01-29 22:00:14 +00:00
r00ster91
aac2d6b56f std.builtin: rename Type.UnionField and Type.StructField's field_type to type 2022-12-17 14:11:33 +01:00
Ryan Liptak
88d927a511 std.debug.TTY: Fix colors not resetting on Windows
This fixes a regression introduced in #12298 where colors would never reset in a Windows console because the attributes would be queried on every `setColor` call, and then try to 'reset' the attributes to what it just queried (i.e. it was essentially doing a complicated no-op on .Reset).

This fixes the problem while (I think) keeping with the spirit of the changes in #12298--that is, `TTY.Config` is not specifically tied to stderr like it was before #12298. To that end, detectTTYConfig now takes a `File` and that's what gets used to query the initial attributes to reset to.

(for context, before #12298, the first `setColor` call is where the reset attributes would get queried and it would always use stderr to do it)
2022-12-15 03:49:26 -05:00
Andrew Kelley
6378644d4e
Merge pull request #13907 from Vexu/call-merge
Remove `stack` option from `@call`
2022-12-13 18:15:18 -05:00
Veikka Tuominen
08b2d491bc update usages of @call 2022-12-13 13:14:20 +02:00
r00ster91
d8b4588d5f fix(terminal): handle some possible errors and resolve TODOs 2022-12-13 09:11:30 +01:00
Veikka Tuominen
9d93b2ccf1 Eliminate BoundFn type from the language
Closes #9484
2022-12-09 20:37:18 -07:00
Ryan Liptak
71c82393eb std.testing: Fully absorb expectEqualBytes into expectEqualSlices
- In #13720, expectEqualBytes was added as a standalone function
- In #13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
2022-12-09 04:36:27 -05:00
Veikka Tuominen
92f1a29c40 AstGen: make @compileError operand implicitly comptime
This matches the language reference.
2022-12-07 14:48:24 +02:00
Ryan Liptak
c37afa2811 std.testing: Improve expectEqualBytes for large inputs and make expectEqualSlices use it
`expectEqualBytes` will now truncate the hexdump of each input to a maximum window of 256 bytes, which makes it safe to use for arbitrarily large inputs. Therefore, it can be used in `expectEqualSlices` when the type is u8.
2022-12-01 02:18:51 -05:00
Ryan Liptak
34fa6a1e04 std.testing: Add expectEqualBytes that outputs hexdumps with diffs highlighted in red
The coloring is controlled by `std.debug.detectTTYConfig` so it will be disabled when appropriate.
2022-11-30 18:57:37 -05:00
Cody Tapscott
a0af3c7a19 std.os: Fix std.os.chdir for WASI
Test coverage was lacking for chdir() on WASI, allowing this to
regress.

This change makes os.chdir() compile again, and improves the test
logic to use our standard CWD support for WASI.
2022-11-26 22:06:15 +02:00
Nick Cernis
8a5818535b
Make invalidFmtError public and use in place of compileErrors for bad format strings (#13526)
* Export invalidFmtErr

To allow consistent use of "invalid format string" compile error
response for badly formatted format strings.

See https://github.com/ziglang/zig/pull/13489#issuecomment-1311759340.

* Replace format compile errors with invalidFmtErr

- Provides more consistent compile errors.
- Gives user info about the type of the badly formated value.

* Rename invalidFmtErr as invalidFmtError

For consistency. Zig seems to use “Error” more often than “Err”.

* std: add invalid format string checks to remaining custom formatters

* pass reference-trace to comp when building build file; fix checkobjectstep
2022-11-12 21:03:24 +02:00
r00ster91
654e0b6679 fix(text): hyphenation and other fixes 2022-10-05 21:19:10 +02:00
r00ster91
f6312e4b69 docs: minor improvements
Just some minor improvements when passing by.
2022-09-29 21:40:25 +02:00
r00ster91
fda0b2c372 fix(std.testing.refAllDeclsRecursive): silently return if !builtin.is_test 2022-09-29 21:40:25 +02:00
Jacob Young
5e0d8a435e testing: fix copy paste typo 2022-09-29 14:39:10 +03:00
Martin Wickham
5523e2061b Move std.testing.zig_exe_path into build options 2022-09-14 14:42:58 -07:00
r00ster
8f3ab96b0e
std.testing: make the caret indicator line more helpful 2022-07-25 15:51:22 +03:00
Ali Chraghi
0b4a3ec950 std: compile error on invalid testing allocator usage 2022-07-24 11:57:00 +03:00
alichraghi
9c66fdadc7 std.testing: add refAllDeclsRecursive function 2022-07-16 12:33:25 +03:00
Ryan Liptak
43770c0103 Fix checkAllAllocationFailures being too strict when checking arg types
Before this would fail to compile:

```
fn testFn(alloc: std.mem.Allocator, arr: []const u8) !void {
    _ = alloc;
    _ = arr;
}

test "checkAll" {
    var arr = [_]u8{ 1, 2, 3 };
    try std.testing.checkAllAllocationFailures(std.testing.allocator, testFn, .{arr[0..]});
}
```

with the error `error: Unexpected type for extra argument at index 0: expected []const u8, found *[3]u8`

By removing this strict equality check, we allow the type checking to be done during the `@field(args, arg_i_str) = @field(extra_args, field.name);` instead, which then allows for things like type coercion to work, but still will give a compile error if the types are incorrect. So, after this change, the above succeeds (because `*[3]u8` can be coerced to `[]const u8`).

The new compile error when providing an incorrect type that can't be coerced looks like this:

```
zig/lib/std/testing.zig:639:35: error: expected type '[]const u8', found '*[3]u32'
        @field(args, arg_i_str) = @field(extra_args, field.name);
                                  ^
```
2022-07-16 12:32:20 +03:00
Veikka Tuominen
262f4c7b3a std.fs: remove OpenDirOptions.iterate 2022-07-15 14:39:21 +03:00
Ryan Liptak
c321b2f2a0 checkAllAllocationFailures: add possibility of SwallowedOutOfMemoryError (split from NondeterministicMemoryUsage)
Inducing failure but not getting OutOfMemory back is not as much of a problem as never inducing failure when it was expected to be induced, so treating them differently and allowing them to be handled differently by the caller is useful.

For example, the current implementation of `std.HashMapUnmanaged.getOrPutContextAdapted` always tries to grow and then recovers from OutOfMemory by attempting a lookup of an existing key. If this function is used (i.e. from `std.BufMap.putMove`) with `checkAllAllocationFailures`, then we'd have previously triggered `error.NondeterministicMemoryUsage`, but the real cause is that `OutOfMemory` is being recovered from and so the error is being swallowed. The new error allows us to both understand what's happening easier and to catch it and ignore it if we're okay with the code we're testing handling `error.OutOfMemory` without always bubbling it up.
2022-06-23 17:20:24 -07:00
Ryan Liptak
def304a9a5 Integrate FailingAllocator stack trace with testing.checkAllAllocationFailures 2022-06-23 17:02:29 -07:00
Andrew Kelley
67d5bfefba std.testing: remove tight coupling with executing zig as child process
This tight coupling causes problems for various targets, requires
hacky "get args" functionality, and bungles relative file system paths,
making invalid assumptions about the zig-cache directory.

In short, these are not unit tests; these should be standalone tests
instead.

Reverts e5d4a694ea
Reverts d976456ef6
Reverts dbbda0f41a
Closes #11542
2022-05-26 16:22:47 -07:00
Jimmi Holst Christensen
a0a2ce92ca std: Do not allocate the result for ChildProcess.init
Instead, just return ChildProcess directly. This structure does not
require a stable address, so we can put it on the stack just fine. If
someone wants it on the heap they should do.

  const proc = try allocator.create(ChildProcess);
  proc.* = ChildProcess.init(args, allocator);
2022-04-29 22:50:34 -04:00
Andrew Kelley
c5e847744c Revert "Merge pull request #11214 from iddev5/ay-build-runner"
This reverts commit 75c9936737, reversing
changes made to 7f13f5cd5f.

I don't think `runZigBuild` belongs in std.testing. We already have
`test/standalone/*` for this.

Additionally test names should explain what they are testing rather than
referencing GitHub issue numbers.
2022-04-28 14:12:32 -07:00
Andrew Kelley
3052597a73 Revert "std.testing: add writeZigFile for TmpDir"
This reverts commit 7f13f5cd5f.

I'd like to review this one before it goes in. This is an awfully
specific API that I don't think belongs in std.testing. Also I don't
want any code snippets in doc strings. We have doctests for that.
2022-04-28 11:20:53 -07:00
Andrew Kelley
360ecc1a2f
Merge pull request #11532 from ziglang/compiler-rt-math
compiler-rt math functions reorg
2022-04-28 13:34:38 -04:00
Veikka Tuominen
75c9936737
Merge pull request #11214 from iddev5/ay-build-runner
std: explicitly handle error.UnexpectedExitCode in build_runner
2022-04-28 18:38:44 +03:00
matu3ba
7f13f5cd5f
std.testing: add writeZigFile for TmpDir
* remove need for manual string concatenation for building binaries in test blocks
* include small program snippet to show how to get binary path with subslicing
2022-04-28 18:37:30 +03:00
Andrew Kelley
41dd2beaac compiler-rt: math functions reorg
* unify the logic for exporting math functions from compiler-rt,
   with the appropriate suffixes and prefixes.
   - add all missing f128 and f80 exports. Functions with missing
     implementations call other functions and have TODO comments.
   - also add f16 functions
 * move math functions from freestanding libc to compiler-rt (#7265)
 * enable all the f128 and f80 code in the stage2 compiler and behavior
   tests (#11161).
 * update std lib to use builtins rather than `std.math`.
2022-04-27 12:20:44 -07:00
Cody Tapscott
7b090df668 stdlib std.os: Improve wasi-libc parity for WASI CWD emulation
Two major changes here:
  1. We store the CWD as a simple `[]const u8` and lookup Preopens for
     every absolute or CWD-referenced file operation, based on the
     Preopen with the longest match (i.e. most specific path)
  2. Preorders are normalized to POSIX absolute paths at init time.
     Behavior depends on the "cwd_root" parameter of `initPreopensWasi`:

	`cwd_root` is used for any Preopens that start with "."

	  For example:
            "./foo/bar" - inits to -> "{cwd_root}/foo/bar"
            "foo/bar"   - inits to -> "/foo/bar"
	    "/foo/bar"  - inits to -> "/foo/bar"

        `cwd_root` must be an absolute path.

	Using "/" as `cwd_root` gives behavior similar to wasi-libc.
2022-04-16 18:08:05 +02:00
iddev5
8d3e7aa5e0 std.testing: add function zigBuild for running zig build runner commands 2022-04-04 18:33:47 +05:30
Ryan Liptak
6d04ab6d5b Add std.testing.checkAllAllocationFailures
Adds a function that allows checking for memory leaks (and other problems) by taking advantage of the FailingAllocator and inducing failure at every allocation point within the provided `test_fn` (based on the strategy employed in the Zig parser tests, which can now use this function).
2022-04-04 15:32:43 +03:00
matu3ba
dbbda0f41a
std.testing: add methods tmpDirPath, getTestArgs, buildExe
continuation of #11093 to simplify testing IPC

* use cases
  - get path to temporary directory
  - get the test arguments inside test block for reusage
  - build executables from text within test blocks, ie to test IPC
* missing conventions
  - how to name and debug test cases
  - where do simple+repititve build commands for testing belong
2022-03-27 11:43:40 +03:00
Andrew Kelley
f3f5a5d05b stage2: improve @typeName
* make it always return a fully qualified name. stage1 is inconsistent
   about this.
 * AstGen: fix anon_name_strategy to correctly be `func` when anon type
   creation happens in the operand of the return expression.
 * Sema: implement type names for the "function" naming strategy.
 * Put "enum", "union", "opaque", or "struct" in place of "anon" when
   creating respective anonymous Decl names.
 * std.testing: add `expectStringStartsWith`. Didn't end up using it
   after all.

Also this enables the real test runner for stage2 LLVM backend (sans
wasm32) since it works now.
2022-03-18 00:12:22 -07:00
Andrew Kelley
9ed3eb9cde std.testing: fix incorrect docs that mentioned aborting
At some point we changed these functions to return errors instead of
aborting.
2022-03-17 17:23:22 -07:00
Benjamin San Souci
e3c2cc1443
std.json: correctly handle sentinel terminated slices 2022-03-08 20:43:13 +02:00