-
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
base: master
Are you sure you want to change the base?
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
6ca4eae
to
6528955
Compare
Likely depends on #3343 to be merged |
c24d5fc
to
c4702ba
Compare
Marking as "ready for review" since most of the code should be there for option_env support |
c4702ba
to
0e3e856
Compare
b80133e
to
c4702ba
Compare
Should be ready after #3366 is integrated |
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
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
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
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
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 (), |
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
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
gcc/rust/ChangeLog: * ast/rust-collect-lang-items.cc: Allow for the collection of EnumItem lang-items * ast/rust-collect-lang-items.h: ... * ast/rust-path.h: Change get_segments function to create an empty path for Lang items, which is useful for preventing various AST passes from recursing into the "segments" of a lang item path. * 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 * hir/tree/rust-hir-path.h: Refactor HIR::PathInExpression to allow for lang items. * hir/rust-ast-lower-expr.cc: ... * hir/tree/rust-hir-path.cc: ... * resolve/rust-ast-resolve-path.cc: Add NR1.0 resolution support for lang-item paths. * typecheck/rust-hir-type-check-path.cc: Add type resolution support for lang-item paths. * typecheck/rust-hir-type-check-expr.h: Add prototype for helper function. * backend/rust-compile-resolve-path.cc: Add compile support for lang-item paths. * checks/lints/rust-lint-marklive.cc: Add if guard to prevent recursion into a lang-item path's empty 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 * rust/compile/nr2/exclude: Some issues with nr2 need to be resolved before allowing option_env with NR2.0. Signed-off-by: Liam Naddell <[email protected]>
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)
Half of this PR is option_env! , half is adding support for compiling lang-item PathInExpression's
NOTE: Testing for lang-item paths is done by testing option_env!. I'm not sure if there's any other way to test compiler-generated paths.
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