-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support user-defined implicit conversions via ImplicitAs
#1273
Merged
zygoloid
merged 31 commits into
carbon-language:trunk
from
zygoloid:explorer-implicitas
May 21, 2022
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fbf33ae
Support `impl as` in generic classes.
zygoloid 43ed1fc
Start implementing ImplicitAs.
zygoloid 294c6f7
Implicit conversion for `while` conditions.
zygoloid f0c8e3d
Split TypeCheckExp into a function to recursively type-check operands…
zygoloid 6f66126
Use single-expression typechecking to avoid skip_typechecking_expr hack.
zygoloid bbf24ab
Factor out builtin tracking into a separate class.
zygoloid 2aaf688
Merge branch 'explorer-impl-as-in-generic-class' into explorer-implic…
zygoloid cb431de
Support deducing the parameters of a parameterized interface.
zygoloid 81e9791
Autoformat
zygoloid e25a1ef
Support for implicit conversion on left of `.`.
zygoloid 6fb8771
Support implicit conversion in condition of `if` expression.
zygoloid 00d3bec
Suppot implicit conversions on the RHS of `=`.
zygoloid e71e814
Support implicit conversion for the condition of an `if` statement.
zygoloid 8de4baa
Support implicit conversion in a return statement.
zygoloid d9cba7e
Support implicit conversion in variable initializers.
zygoloid 9dc0fa1
Support implicit conversions when pattern-matching a choice value.
zygoloid 995862e
Support implicit conversions in `match` statements.
zygoloid 3534f00
Support implicit conversion in the operand of `__run`.
zygoloid 566f58b
Make user-defined implicit conversions less optional.
zygoloid d142d52
Reformat.
zygoloid c6349ee
Fix build failure in fuzzer from new ValueLiteral node.
zygoloid 069f527
Merge branch 'trunk' into explorer-implicitas
zygoloid eee2e57
Revert "Split TypeCheckExp into a function to recursively type-check …
zygoloid d2af4ad
Responses to review comments.
zygoloid 26239fc
Add test for missing parens in choice pattern match.
zygoloid dcbaf0d
Add missing %{not}s.
zygoloid bd6263d
Merge branch 'trunk' into explorer-implicitas
zygoloid defdf2f
Fix test failure after merge: basic support for deduction of non-type…
zygoloid 2402ccf
Merge branch 'trunk' into explorer-implicitas
zygoloid 30894b3
More fallout from adding tests covering non-type class and interface
zygoloid a7a6ddd
Merge branch 'trunk' into explorer-implicitas
zygoloid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "explorer/interpreter/builtins.h" | ||
|
||
#include "explorer/common/error_builders.h" | ||
|
||
using llvm::dyn_cast; | ||
|
||
namespace Carbon { | ||
|
||
void Builtins::Register(Nonnull<const Declaration*> decl) { | ||
if (auto* interface = dyn_cast<InterfaceDeclaration>(decl)) { | ||
if (interface->name() == GetName(Builtin::ImplicitAs)) { | ||
builtins_[static_cast<int>(Builtin::ImplicitAs)] = interface; | ||
} | ||
} | ||
} | ||
|
||
auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const | ||
-> ErrorOr<Nonnull<const Declaration*>> { | ||
std::optional<const Declaration*> result = | ||
builtins_[static_cast<int>(builtin)]; | ||
if (!result.has_value()) { | ||
return CompilationError(source_loc) | ||
<< "missing declaration for builtin `" << GetName(builtin) << "`"; | ||
} | ||
return result.value(); | ||
} | ||
|
||
} // namespace Carbon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef CARBON_EXPLORER_INTERPRETER_BUILTINS_H_ | ||
#define CARBON_EXPLORER_INTERPRETER_BUILTINS_H_ | ||
|
||
#include <optional> | ||
|
||
#include "common/error.h" | ||
#include "explorer/ast/declaration.h" | ||
#include "explorer/ast/expression.h" | ||
#include "explorer/common/nonnull.h" | ||
#include "explorer/common/source_location.h" | ||
#include "explorer/interpreter/value.h" | ||
|
||
namespace Carbon { | ||
|
||
class Builtins { | ||
public: | ||
explicit Builtins() {} | ||
|
||
enum class Builtin { ImplicitAs, Last = ImplicitAs }; | ||
// FIXME: In C++20, replace with `using enum Builtin;`. | ||
static constexpr Builtin ImplicitAs = Builtin::ImplicitAs; | ||
|
||
// Register a declaration that might be a builtin. | ||
void Register(Nonnull<const Declaration*> decl); | ||
|
||
// Get a registered builtin. | ||
auto Get(SourceLocation source_loc, Builtin builtin) const | ||
-> ErrorOr<Nonnull<const Declaration*>>; | ||
|
||
// Get the source name of a builtin. | ||
static constexpr auto GetName(Builtin builtin) -> const char* { | ||
return BuiltinNames[static_cast<int>(builtin)]; | ||
} | ||
|
||
private: | ||
static constexpr int NumBuiltins = static_cast<int>(Builtin::Last) + 1; | ||
static constexpr const char* BuiltinNames[NumBuiltins] = {"ImplicitAs"}; | ||
|
||
std::optional<Nonnull<const Declaration*>> builtins_[NumBuiltins] = {}; | ||
}; | ||
|
||
} // namespace Carbon | ||
|
||
#endif // CARBON_EXPLORER_INTERPRETER_BUILTINS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where you're adding setters, is there anything this can do relative to checking type checking of rhs_ or rhs that would help enforce the "Can only"? Maybe it would make sense to do something like "set_desugared_rhs" which could only be called once, and a desugared_rhs getter that returns desugared_rhs if set, rhs otherwise?
Note, I'm wondering about this mainly in the larger context of adding setters. This could go a step further with a templated type, e.g.:
Then maybe something like this becomes:
However, that's a much higher code delta, especially because it affects interpreter call sites. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is probably a better direction here, but I think it's likely to take a bit of discussion to hammer out the best approach. Do you want this addressed before this change lands, or can this be deferred to a later refactoring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's probably reasonable to defer to a later refactoring, that was part of my concern over size of the delta.