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

allow implicit cast from S to ?&const S #785

Merged
merged 1 commit into from Feb 26, 2018
Merged

allow implicit cast from S to ?&const S #785

merged 1 commit into from Feb 26, 2018

Conversation

bnoordhuis
Copy link
Contributor

Allow implicit casts from container types to nullable const pointers to
said container type. That is:

fn f() void {
    const s = S {};
    g(s);   // Works.
    g(&s);  // So does this.
}

fn g(_: ?&const S) void {  // Nullable const pointer.
}

Fixes #731.

Allow implicit casts from container types to nullable const pointers to
said container type.  That is:

    fn f() void {
        const s = S {};
        g(s);   // Works.
        g(&s);  // So does this.
    }

    fn g(_: ?&const S) void {  // Nullable const pointer.
    }

Fixes #731.
@bnoordhuis
Copy link
Contributor Author

bnoordhuis commented Feb 23, 2018

I should mention that it doesn't work for pointers-to-pointers. This is still an error:

const s = S {};
const p = &s;
f(&p);  // fn f(_: ?&const ?&const S) void {}

Is that a safe transformation to make?

edit: Poorly phrased question. Better question: are non-nullable pointers a compile-time thing only? No effect on the size of the object?

@bnoordhuis
Copy link
Contributor Author

Oh, and another question: should this work?

const s = S {};
try f(&s);  // fn f(_: error!?&const S) !void {}

I ask because it doesn't work with error!&const S either. error!S does however.

@andrewrk andrewrk merged commit 1eecfda into ziglang:master Feb 26, 2018
@andrewrk
Copy link
Member

are non-nullable pointers a compile-time thing only? No effect on the size of the object?

Correct. Nullable pointers are guaranteed to be pointer sized.

I think that the example with an error set should work too.

Related is #733. After this is done we might remove the ability to cast T to &const T.

@andrewrk
Copy link
Member

@bnoordhuis,

Once again, thank you for a high quality, high impact pull request. Would you like to become one of the owners of the zig-lang organization and have commit access?

@bnoordhuis
Copy link
Contributor Author

Correct. Nullable pointers are guaranteed to be pointer sized. I think that the example with an error set should work too.

Okay, I'll chip at it some more.

Would you like to become one of the owners of the zig-lang organization and have commit access?

I'd be honored!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants