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

remove type coercion from array values to references #3768

Closed
andrewrk opened this issue Nov 25, 2019 · 1 comment
Closed

remove type coercion from array values to references #3768

andrewrk opened this issue Nov 25, 2019 · 1 comment
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

Now that #265 is done, the main motivation for type coercion from array values to slices is gone. It's a footgun for Zig to automatically convert a value into a pointer to that value; such an operation should be explicit.

test "coerce array value to slice" {
    var array: []const i32 = [_]i32{ 1, 2, 3, 4 };
}

Expected output:

test.zig:2:30: error: expected type '[]const i32', found '[4]i32'
    var array: []const i32 = [_]i32{ 1, 2, 3, 4 };
                                   ^

Actual output: (test passes)

How to upgrade code for these new semantics:

-    var array: []const i32 = [_]i32{ 1, 2, 3, 4 };
+    var array: []const i32 = &[_]i32{ 1, 2, 3, 4 };

I expect this change to simplify the result location semantics, which should help with reasoning about zig code, as well as reduce the complexity of a zig compiler.

@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Nov 25, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Nov 25, 2019
@andrewrk andrewrk changed the title Remove type coercion from array values which do not have static lifetimes to references remove type coercion from array values to references Nov 25, 2019
andrewrk added a commit that referenced this issue Nov 27, 2019
 * Implements #3768. This is a sweeping breaking change that requires
   many (trivial) edits to Zig source code. Array values no longer
   coerced to slices; however one may use `&` to obtain a reference to
   an array value, which may then be coerced to a slice.

 * Adds `IrInstruction::dump`, for debugging purposes. It's useful to
   call to inspect the instruction when debugging Zig IR.

 * Fixes bugs with result location semantics. See the new behavior test
   cases, and compile error test cases.

 * Fixes bugs with `@typeInfo` not properly resolving const values.

 * Behavior tests are passing but std lib tests are not yet. There
   is more work to do before merging this branch.
@andrewrk
Copy link
Member Author

andrewrk commented Dec 2, 2019

Implemented in #3787, landed in fecd540.

@andrewrk andrewrk closed this as completed Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. 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

1 participant