Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't stop compilation if compileLog is used #5469

Open
data-man opened this issue May 29, 2020 · 11 comments
Open

Don't stop compilation if compileLog is used #5469

data-man opened this issue May 29, 2020 · 11 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@data-man
Copy link
Contributor

@compileLog is a very useful auxiliary function and I wonder why the compilation should stop.

@data-man
Copy link
Contributor Author

Note: when #5348 will merged this issue will have a special meaning.

@squeek502
Copy link
Collaborator

squeek502 commented May 30, 2020

I think having it fail compilation by default is good since it then encourages library developers, etc to keep @compileLog out of their libraries, but it would be nice to have some sort of option (like a compile flag) to allow compilation to succeed with @compileLog statements for local debugging purposes.

@pixelherodev
Copy link
Contributor

compileLog erroring and the lack of warnings are both deliberate design decisions. If something isn't enough of a priority to warrant an error, it should not be printed out IMO.

@data-man
Copy link
Contributor Author

Another proposals:

  • add logLevel parameter to @compilerLog
  • @compilerError == @compilerLog(.Error, message)
  • add compilerLog(comptime logLevel, comptime values: var) to std.debug
  • add compilerError(comptime values: var) to std.debug
  • etc. (e.g. compilerInfo, compilerWarn, ...)

@CurtisFenner
Copy link

I don't think separating the @compilerLogs into "log levels" makes sense.

As others have mentioned, Zig is choosing to avoid a concept of "warning", and instead encourage libraries to make bad uses illegal. (I think it's unlikely you can detect most bad uses at comptime anyway, you'd most likely need a wider static analysis)

In addition, it could get noisy. Some libraries may more liberally use some log levels, and you probably don't care about most of them when you're "raising" the level of logs printed.

I do think there are times when there are "interesting" logs that don't indicate a problem that you might still sometimes want to inspect, perhaps when something is behaving strangely or you're trying to figure out all of the ways a comptime library is being used.

Perhaps something like this:

  • Rename @compileLog to @compileError
  • Add @compileLog(tag: []const u8, message: []const u8).
    By default, this does nothing.
    But, when compiling with a flag --show-compile-logs tag with a matching tag they will appear. That way you could, for example, --show-compile-logs special-dsl-debugging if you want to get a more detailed view of what a complicated comptime DSL library is doing.

On the other hand, using a tag might be a mistake, since it might encourage everyone to just use a tag called "warn". So alternatively it could be filtered to a source file, like --show-compile-logs library/special-dsl.zig, which would only show @compileLogs emitted from within that DSL library.

It might also be interesting to allow @compileError to optionally dump all of the logs associated with a given tag/file that were made up to that point, so that complicated libraries can show more context without having to do a lot of comptime log accounting.

And perhaps like the existing @compileLog, maybe compiling with --show-compile-log would not produce any artifacts/run any tests (to encourage you to only run it when debugging a problem and not as a warning system).

@SpexGuy
Copy link
Contributor

SpexGuy commented May 30, 2020

Another important benefit is that the current behavior enables incremental compilation. A major drawback of incremental compilation in other languages is that you don't see warnings for compilation units that don't get recompiled. Since Zig's unit of incremental compilation is a declaration, it's important that we don't have this problem. We solve it by not having warnings, so either something is printed and compilation fails or compilation succeeds and nothing is printed. This behavior has to extend to compileLog as well, in order to see this benefit.

@data-man
Copy link
Contributor Author

The explanation step by step:

  • I need to debug the comptime variables
  • and I'm using @compileLog for that.
  • but I can't test the application because the compilation is stopped
  • so I've commented all lines that contains @compileLog

The important benefit is that @compileLog's messages aren't displayed in the compiled application.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jun 3, 2020
@Vexu Vexu added this to the 0.7.0 milestone Jun 3, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@g-w1
Copy link
Contributor

g-w1 commented Feb 16, 2021

This is not the case fixed in stage2 right now.

@g-w1
Copy link
Contributor

g-w1 commented Feb 16, 2021

Sorry, it doesn't happen in stage2 right now:
$ cat test.zig
export fn _start() noreturn {
func();
@compilelog(.here);
unreachable;
}

fn func() void {
@compilelog(.here2);
}
$ ./zig build-exe test.zig -fno-LLVM
test.zig:3:5: error: found compile log statement
test.zig:8:5: note: also here

Compile Log Output:
@as(@type(.EnumLiteral), .here)
@as(@type(.EnumLiteral), .here2)
$

It errors out at the end of compilation (but still finishes), but a special compile log buffer is kept that stores the output. Is the best of both worlds IMO. And the error is only shown if there are no other compilation errors.

@squeek502
Copy link
Collaborator

That's the same behavior as stage1, just a different format:

$ zig build-exe 5469.zig
| here
| here2
./5469.zig:3:5: error: found compile log statement
    @compileLog(.here);
    ^
./5469.zig:8:5: error: found compile log statement
    @compileLog(.here2);
    ^

This proposal is to (imo, optionally) make it possible to stop the error: found compile log statement compile errors from occuring (i.e. allow the build to succeed, emit the resulting exe, etc).

@g-w1
Copy link
Contributor

g-w1 commented Feb 16, 2021

Ah sorry for the misunderstanding, but with the incremental model of stage2, it will emit an exe even with these compile log errors. (At least I think it should ...)
Ignore my previous comments, I was operating off false memory, sorry about that. This would be very easy to implement in stage2 though.

@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

8 participants