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

Initial support for aliases. #1262

Merged
merged 9 commits into from
May 16, 2022
Merged

Initial support for aliases. #1262

merged 9 commits into from
May 16, 2022

Conversation

zygoloid
Copy link
Contributor

@zygoloid zygoloid commented May 13, 2022

This supports aliases for types (including interfaces), functions, parameterized types, instance member names, and interface member names.

@zygoloid zygoloid requested a review from a team as a code owner May 13, 2022 00:28
explorer/ast/declaration.cpp Outdated Show resolved Hide resolved
@@ -2169,6 +2169,72 @@ auto TypeChecker::TypeCheckChoiceDeclaration(
return Success();
}

static bool IsValidAliasTarget(Nonnull<const Value*> type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually checking the type of the alias target, not the alias target itself. This nuance is a little misleading for the alias x = i32 case where I'd say the target is i32, not type(i32), but this gets called with type(i32). Maybe rename+comment the function, maybe IsValidAliasType or CanAliasType?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.


package ExplorerTest api;

alias TypeAlias = i32;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you switch most of these to separate files? This setup is okay when everything's passing, but if something breaks, it'd be easier to figure out what's broken with smaller tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@zygoloid zygoloid added explorer Action items related to Carbon explorer code and removed DO NOT MERGE labels May 14, 2022
var c: GenericClass(i32) = {};
var d: GenericClassAlias(i32) = c;
var e: ClassSpecializationAlias = c;
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, you might want to put a variable on your classes and return the result or something, to verify that data is tracked properly with an alias. In another test you did:

class Class { var a: i32; }
class GenericClass(T:! Type) { var a: T; }

That's really all I'm suggesting here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

fn Main() -> i32 {
var ab: AB = {.b = 1, .a = 2};
var ba: BA = ab;
return 1000 * ab.a + 100 * ab.b + 10 * ba.a + ba.b;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe drop TODOs on these to use print() if we can get that supporting ints in output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@zygoloid zygoloid merged commit 471bb56 into carbon-language:trunk May 16, 2022
chandlerc pushed a commit that referenced this pull request Jun 28, 2022
This supports aliases for types (including interfaces), functions, parameterized types, instance member names, and interface member names.

Co-authored-by: Jon Meow <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
explorer Action items related to Carbon explorer code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants