-
Notifications
You must be signed in to change notification settings - Fork 165
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
Add option_env support #3094
Add option_env support #3094
Conversation
6262158
to
62a89ef
Compare
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.
honestly, well done - this isn't an easy part of the codebase, and you did a really good job :) don't worry about the tokenstream stuff, we can think about fixing this later. for the path issue, you're hitting a very frustrating issue which I'm trying to fix in #3068. ideally, we should be able to create paths to lang items (such as Option::Some
and Option::None
) so that we don't care about the module structure, or the name, or anything - we just care about hitting the proper type from the standard library. good work!
Thank you :) |
8350b2c
to
dd8612c
Compare
36cd2a3
to
2034102
Compare
74ad9b9
to
1f41fa7
Compare
@CohenArthur Should I update this PR to use the lang-item paths from #3068 ? |
1f41fa7
to
3f39ce6
Compare
ca99a70
to
962dc4d
Compare
I've been trying to catch up on that issue (sorry for the delay!). We should probably rely on lang item path because right now the tests make use of a |
Ok, I can wait until the lang items path stuff is usable. If appropriate, we can also revisit some of the other macros then as well to integrate lang items. |
Converting to a Draft PR until we have lang items integrated. |
962dc4d
to
6ca4eae
Compare
ef93339
to
1596237
Compare
if (tl::optional<LangItem::Kind> lang_item = get_lang_item_attr (item)) | ||
{ | ||
rust_debug ("[CollectLangItems] Adding lang item %s", | ||
LangItem::ToString (*lang_item).c_str ()); | ||
mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ()); | ||
} |
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'd say remove this, but feel free to keep it if you think it helps
gcc/rust/ast/rust-path.h
Outdated
rust_assert (kind == Kind::Regular); | ||
// For lang-items, this returns an empty path. | ||
return segments; |
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 we should keep the original here. Everywhere that calls .get_segments()
where a lang item can also be present need to be handled differently to resolve to the proper lang item anyway, so the assertion makes sense
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.
Ok, I can change this, I think it will require adding some code like:
if (path.is_lang_item())
return;
to various AST passes, since a lot of these passes try to recurse into a PathInExpression's segments
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.
Fixed
gcc/rust/ast/rust-path.h
Outdated
rust_assert (kind == Kind::Regular); | ||
// For lang-items, this returns an empty path. |
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.
Likewise
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.
Fixed
gcc/rust/hir/tree/rust-hir-path.h
Outdated
std::vector<PathExprSegment> path_segments, | ||
tl::optional<LangItem::Kind> lang_item = tl::nullopt, |
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'd like to see two constructors, one with segments and one with the lang item. Is that code I wrote or is it something you added for this PR?
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 added all lang item handling at/below HIR level for PathInExpression.
I can fix this, this was a hack I forgot to remove. I'll try to handle HIR::PathInExpression similarly to AST::PathInExpression as far as it relates to constructors
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.
Fixed
gcc/rust/hir/rust-ast-lower.cc
Outdated
= new HIR::PathInExpression (mapping, std::move (path_segments), | ||
expr.get_lang_item (), expr.get_locus (), |
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.
so we'd get rid of the segments here for example
= new HIR::PathInExpression (mapping, std::move (path_segments), | |
expr.get_lang_item (), expr.get_locus (), | |
= new HIR::PathInExpression (mappin, expr.get_lang_item (), expr.get_locus (), |
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.
Fixed
void | ||
TypeCheckExpr::handle_enum_lang_item (HIR::PathInExpression &expr) | ||
{ | ||
// Find the lang item's definition | ||
Analysis::Mappings &mappings = Analysis::Mappings::get (); | ||
NodeId lang_item_node = mappings.get_lang_item_node (expr.get_lang_item ()); | ||
tl::optional<HirId> ohid = mappings.lookup_node_to_hir (lang_item_node); | ||
rust_assert (ohid != tl::nullopt); | ||
HirId hid1 = *ohid; | ||
std::pair<HIR::Enum *, HIR::EnumItem *> ohi | ||
= mappings.lookup_hir_enumitem (hid1); | ||
|
||
HIR::Enum &enum_def = *(ohi.first); | ||
HIR::EnumItem &variant_def = *(ohi.second); | ||
|
||
TyTy::BaseType *bl = TypeCheckItem::Resolve (enum_def); | ||
TyTy::VariantDef *vde | ||
= TypeCheckEnumItem::Resolve (variant_def, INT64_MAX - 1); | ||
|
||
context->insert_variant_definition (expr.get_mappings ().get_hirid (), | ||
vde->get_id ()); | ||
bl = SubstMapper::InferSubst (bl, expr.get_locus ()); | ||
resolver->insert_resolved_misc (expr.get_mappings ().get_nodeid (), | ||
expr.get_mappings ().get_nodeid ()); | ||
|
||
infered = bl; | ||
rust_assert (bl != nullptr); | ||
return; | ||
} |
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.
@philberty how does that look? you know the typechecker better than me lol
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.
As a note, I'm looking at this code again, and it seems like some of the assert's might want to be compile errors instead maybe. I'm thinking of the case where somebody puts #[lang = 'Some']
on something that's not an enum variant
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.
that's fine, because having "wrong lang items" is such a corner case that we don't really need to handle it. we know that core
is correct code, if someone wants to reimplement their own core
then the burden falls on them rather than on the compiler. asserts are better IMO
Also I see the build-and-check with glibcxx assertions turned on ICE's. I'll try to figure out what's going on there. (gcc is giving me trouble with compiling :/ ) |
might be because we're doing something like |
Because of my env, I was compiling gccrs with gccrs, which didn't work, because I think gccrs' gcc wasn't properly configured. I changed to use system gcc and it compiles perfectly. Your guess was exactly what it was. the ICE was rust-hir-path.cc:266 segments.back () |
1596237
to
8e8fdcc
Compare
TyTy::BaseType *lookup = nullptr; | ||
bool ok | ||
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), | ||
&lookup); | ||
rust_assert (ok); | ||
|
||
tree t = attempt_constructor_expression_lookup (lookup, ctx, | ||
expr.get_mappings (), | ||
expr.get_locus ()); | ||
TREE_USED (t) = 1; | ||
resolved = t; |
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.
@CohenArthur That GLIBCXX assert CI/CD job caught some undefined behavior, where I pushed a random address into the ResolvePathRef::resolve function as the "final segment". I added code here to properly handle lang-item paths.
(Still working on fixing the AST::PathInExpression, I should be able to get that done soon)
a8b5ef0
to
455fff0
Compare
333d52d
to
f50cdf6
Compare
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.
that's great work, looks perfect to me :D thank you so much @liamnaddell !
if (expr.is_lang_item ()) | ||
return; |
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 resolution here should be pretty easy but we'll do this in another PR - I'm happy with that fix
gcc/rust/ChangeLog: * expand/rust-macro-builtins-utility.cc: Add macro expansion for option_env with eager expansion * expand/rust-macro-builtins.cc: Add option_env to builtin list * expand/rust-macro-builtins.h: Add option_env handler to header file * resolve/rust-late-name-resolver-2.0.cc: Prevent NR2.0 from recursing into lang-item segments gcc/testsuite/ChangeLog: * rust/compile/macros/builtin/option_env1.rs: Add success case for option_env * rust/compile/macros/builtin/option_env2.rs: Add failure case for option_env * rust/execute/torture/builtin_macro_option_env.rs: Add execution case for option_env
f50cdf6
to
705c3f6
Compare
Fixes #1806
Addresses #927, #1791
Here is a checklist to help you with your PR.
make check-rust
passes locallyclang-format
gcc/testsuite/rust/
Adds compiler support for option_env, adds eager expansion for option_env, adds tests for option_env