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

implement @as builtin and fix result location semantics with regards to type coercion #3628

Merged
merged 10 commits into from Nov 8, 2019

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Nov 8, 2019

This branch implements #1757. It breaks a lot of code.

It also hooks up variable declarations which have type annotations into result location:

test "example" {
    // master branch: writes result into temporary variable; copies into x.
    // this branch: semantically equivalent to the following line with `y`.
    var x: Point = makePoint(); 
    // master branch and this branch: makePoint writes result directly into y
    var y = makePoint();
}

const Point = struct { x: i32, y: i32 };

fn makePoint() Point {
    return Point { .x = 1, .y = 2};
}

And it hooks up type coercion into result location semantics:

test "example" {
    // master branch: writes result into temporary variable; copies into x.
    // this branch: semantically equivalent to the following line with `y`.
    var x = @as(Point, makePoint());
    // master branch and this branch: makePoint writes result directly into y
    var y = makePoint();
}

This makes it easier to read code, since a function call is always a function call now, and type coercion is a lot easier to spot.

It also makes the language simpler, and therefore the compiler easier to work on. Less room for bugs dealing with the ambiguity of function call syntax possibly being a function call, or type coercion.

Merge Checklist

  • fix tests that regressed
  • update language reference documentation

Bear with me on this change. It'll be worth it. As a consolation prize for dealing with your code breaking, this is a direct prerequisite of anonymous struct literals (#685) which I plan to do next. So after you fix all your type coercions, you'll at least get to have a hot new feature to play with soon after.

@andrewrk andrewrk added the breaking Implementing this issue could cause existing code to no longer compile or have different behavior. label Nov 8, 2019
@@ -61,7 +61,7 @@ pub const State = struct {
}

pub fn squeeze(self: *Self, out: []u8) void {
var i = usize(0);
var i = @as(usize, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better idiom would be var i: usize = 0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. They are semantically identical, therefore the shorter one is preferred.

@JesseRMeyer
Copy link

JesseRMeyer commented Nov 8, 2019

Must the type parameter to @as() be comp-time known? For variably sized stack based allocations, it seems that it must be, but if pointers are all the same size, there is an alluring reason to allow runtime-known pointer type casts from @as(), or a close relative.

For example, maybe you have a single routine that returns a pointer pointing to a vector whose length is that of the active processor.

@andrewrk
Copy link
Member Author

andrewrk commented Nov 8, 2019

@JesseRMeyer I don't understand what you're suggesting - sounds like a proposal for a separate issue, that could use some code examples to help clarify.

This commit also hooks up type coercion (previously called implicit
casting) into the result location mechanism, and additionally hooks up
variable declarations, maintaining the property that:

    var a: T = b;

is semantically equivalent to:

    var a = @as(T, b);

See #1757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants