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

cannot compile with multiline strings #14513

Closed
TheRealDxRed opened this issue Feb 2, 2023 · 2 comments
Closed

cannot compile with multiline strings #14513

TheRealDxRed opened this issue Feb 2, 2023 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@TheRealDxRed
Copy link

Zig Version

0.11.0-dev.1507+6f13a725a

Steps to Reproduce and Observed Behavior

Compile the following source file

const std = @import("std");

pub fn main() void {
    std.debug.print(
        \\Hello World
        \\
        ,
        .{}
    );
}

which will result in an error about a poorly terminated symbol instead of a multi line string.

src\main.zig:5:16: error: expected ',' after argument
  \\Hello World
               ^~~~
src\main.zig:6:3: note: invalid byte: '\\'
  \\
  ^

Expected Behavior

The lines beginning with doubled backslashes should have been read as multiline strings, as per 5.3.2. Multiline String Literals

@TheRealDxRed TheRealDxRed added the bug Observed behavior contradicts documented or intended behavior label Feb 2, 2023
@squeek502
Copy link
Collaborator

squeek502 commented Feb 2, 2023

Can't reproduce this.

$ ./zig-linux-x86_64-0.11.0-dev.1507+6f13a725a/zig run 14513.zig
Hello World

where 14513.zig contains the code from the OP.

EDIT: It's possible that Windows line endings (CRLF) are causing this. If I convert the file to use CRLF line endings, I get a similar compile error:

14513.zig:5:22: error: expected ',' after argument
        \\Hello World
                     ^~~~
14513.zig:6:3: note: invalid byte: ' '
        \\
  ^

Make sure you are using unix-style (LF) line endings. I'm unsure of the status of line-ending stuff, though, so I'm not sure if this is a bad/misleading error message or a bug.

EDIT#2: Was able to get an exact reproduction with hard tabs before the second \\ line in addition to the CRLF line endings:

const std = @import("std");

pub fn main() void {
    std.debug.print(
        \\Hello World
<\t><\t>\\
        ,
        .{}
    );
}

where <\t> are hard tab characters. See the Source Encoding section of the lang ref for recommendations about avoiding CRLF and hard tabs.


The error message here should definitely be improved, though; additionally, I hit an unreachable when trying to run zig fmt on the file with CRLF line endings:

$ unix2dos 14513.zig
unix2dos: converting file 14513.zig to DOS format...
$ zig fmt 14513.zig
thread 3909774 panic: reached unreachable code
/home/ryan/Programming/zig/zig/lib/std/debug.zig:281:14: 0x989fb6 in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/home/ryan/Programming/zig/zig/lib/std/zig/Ast.zig:120:17: 0xab536c in tokenSlice (zig)
    assert(token.tag == token_tag);
                ^
/home/ryan/Programming/zig/zig/src/main.zig:4678:77: 0xb5c788 in printErrsMsgToStdErr (zig)
            const bad_off = @intCast(u32, tree.tokenSlice(parse_error.token + @boolToInt(parse_error.token_is_prev)).len);
                                                                            ^
/home/ryan/Programming/zig/zig/src/main.zig:4572:83: 0xdc61cb in fmtPathFile (zig)
    try printErrsMsgToStdErr(fmt.gpa, fmt.arena, tree.errors, tree, file_path, fmt.color);
                                                                                  ^
/home/ryan/Programming/zig/zig/src/main.zig:4494:16: 0xb601ec in fmtPath (zig)
    fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) {
               ^
/home/ryan/Programming/zig/zig/src/main.zig:4458:56: 0x9b9c75 in cmdFmt (zig)
        try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path);
                                                       ^
/home/ryan/Programming/zig/zig/src/main.zig:296:22: 0x987fc7 in mainArgs (zig)
        return cmdFmt(gpa, arena, cmd_args);
                     ^
/home/ryan/Programming/zig/zig/src/main.zig:207:20: 0x986b15 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/ryan/Programming/zig/zig/lib/std/start.zig:616:37: 0x9891f7 in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x7fffed1d2082 in ??? (???)
Aborted

@Vexu
Copy link
Member

Vexu commented Feb 2, 2023

Duplicate of #11414

@Vexu Vexu marked this as a duplicate of #11414 Feb 2, 2023
@Vexu Vexu closed this as completed Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants