Skip to content

Does struct field order matter for impl lookup and type structure? #5413

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

Open
danakj opened this issue May 2, 2025 · 1 comment
Open

Does struct field order matter for impl lookup and type structure? #5413

danakj opened this issue May 2, 2025 · 1 comment
Labels
leads question A question for the leads team

Comments

@danakj
Copy link
Contributor

danakj commented May 2, 2025

Summary of issue:

interface Z {
  let X:! type;
}
impl {.a: (), .b: ()} as Z where .X = i32 {}
impl {.b: (), .a: ()} as Z where .X = i64 {}

fn F() {
  let x: auto = 1 as ({.a: (), .b: ()} as Z).X;
  let y: auto = 1 as ({.b: (), .a: ()} as Z).X;
}

Is this an error because the two impls overlap? If not, is the type of x i32 and the type of y i64?

Details:

No response

Any other information that you want to share?

No response

@danakj danakj added the leads question A question for the leads team label May 2, 2025
github-merge-queue bot pushed a commit that referenced this issue May 5, 2025
…nd orders (#5416)

This test demonstrates the scenarios discussed in
#5413
@danakj
Copy link
Contributor Author

danakj commented May 6, 2025

I want to add that structs with different fields orders but the same names are different types, but are implicitly convertible.

https://carbon.godbolt.org/z/hPdvY1e53

fn F() {
    let a: {.a: (), .b: ()} = {.b = (), .a = ()};
}

Here we see two types:

  %struct_type.a.b: type = struct_type {.a: %empty_tuple.type, .b: %empty_tuple.type} [concrete]
  %struct_type.b.a: type = struct_type {.b: %empty_tuple.type, .a: %empty_tuple.type} [concrete]

And a conversion between them:

  %.loc3_48.1: %struct_type.b.a = struct_literal (%.loc3_38, %.loc3_47)
  %struct: %struct_type.a.b = struct_value (%.loc3_48.2, %.loc3_48.3) [concrete = constants.%struct]
  %.loc3_48.4: %struct_type.a.b = converted %.loc3_48.1, %struct [concrete = constants.%struct]

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

No branches or pull requests

1 participant