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

[ #1806 ] Add option_env support #3094

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

liamnaddell
Copy link
Contributor

@liamnaddell liamnaddell commented Jul 22, 2024

Note to reviewers: I do not really get the token stream stuff, I omitted adding a token stream to AST::Fragment, since I wasn't sure how to test the generated token stream.

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

gcc/testsuite/ChangeLog:
	* rust/compile/option_env1.rs: Add success case for option_env
	* rust/compile/option_env2.rs: Add failure case for option_env
	* rust/execute/torture/builtin_macro_option_env.rs: Add execution case for option_env

Fixes #1806

Addresses #927, #1791

Here is a checklist to help you with your PR.


Adds compiler support for option_env, adds eager expansion for option_env, adds tests for option_env

@liamnaddell liamnaddell marked this pull request as draft July 22, 2024 00:50
@liamnaddell liamnaddell marked this pull request as ready for review July 23, 2024 00:07
@liamnaddell liamnaddell force-pushed the option_env branch 3 times, most recently from 6262158 to 62a89ef Compare July 23, 2024 02:12
Copy link
Member

@CohenArthur CohenArthur left a 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!

gcc/rust/expand/rust-macro-builtins-utility.cc Outdated Show resolved Hide resolved
Comment on lines 11 to 18
enum Option<T> {
Some(T),
None,
}
Copy link
Member

Choose a reason for hiding this comment

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

...something like this:

Suggested change
enum Option<T> {
Some(T),
None,
}
mod core {
mod option {
enum Option<T> {
Some(T),
None,
}
}
}

Copy link
Contributor Author

@liamnaddell liamnaddell Jul 24, 2024

Choose a reason for hiding this comment

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

I fixed this for the tests. However, to get the compilation to work, I needed an extra
use core::option::Option;

Is that expected behavior? I would assume this is incorrect behavior, maybe a separate issue?

Copy link
Member

Choose a reason for hiding this comment

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

hmmm... this is weird. you can investigate the generated code by using -frust-dump-all and looking at the generated gccrs.ast-pretty-expanded.dump file. it should show exactly the code you've generated with your macro expansion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems like the expected behavior, since we don't have a prelude at the moment:

liam@gentoo ~/option_env2 $ rustc main.rs
error[E0412]: cannot find type `Option2` in this scope
  --> main.rs:23:13
   |
23 |     let _ : Option2< &'_ str > = core2::option2::Option2::Some("/home/liam/option2_env2");
   |             ^^^^^^^
  --> /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:571:1
   |
   = note: similarly named enum `Option` defined here
   |
help: an enum with a similar name exists
   |
23 |     let _ : Option< &'_ str > = core2::option2::Option2::Some("/home/liam/option2_env2");
   |             ~~~~~~
help: consider importing this enum
   |
9  + use core2::option2::Option2;
   |

error[E0412]: cannot find type `Option2` in this scope
  --> main.rs:24:13
   |
24 |     let _ : Option2< &'_ str > = core2::option2::Option2::None;
   |             ^^^^^^^
  --> /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:571:1
   |
   = note: similarly named enum `Option` defined here
   |
help: an enum with a similar name exists
   |
24 |     let _ : Option< &'_ str > = core2::option2::Option2::None;
   |             ~~~~~~
help: consider importing this enum
   |
9  + use core2::option2::Option2;
   |

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0412`.
liam@gentoo ~/option_env2 $ cat main.rs
//#![feature(rustc_attrs)]
//#[rustc_builtin_macro] macro_rules! option2_env {
//    () => { };
//}

//#[lang = "sized"]
//trait Sized< > { }

pub mod core2 {
    pub mod option2 {
        pub enum Option2< T > {
            Some(T),
            None,
        }

}

}

//use core2::option2::Option2;

fn main() {
    let _ : Option2< &'_ str > = core2::option2::Option2::Some("/home/liam/option2_env2");
    let _ : Option2< &'_ str > = core2::option2::Option2::None;
}

Copy link
Contributor Author

@liamnaddell liamnaddell Aug 13, 2024

Choose a reason for hiding this comment

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

I did mess up the "pub" on the module names in the test though, This is fixed :)

@liamnaddell
Copy link
Contributor Author

honestly, well done - this isn't an easy part of the codebase, and you did a really good job :)

Thank you :)

@liamnaddell liamnaddell force-pushed the option_env branch 6 times, most recently from 8350b2c to dd8612c Compare August 1, 2024 22:54
@liamnaddell liamnaddell force-pushed the option_env branch 2 times, most recently from 36cd2a3 to 2034102 Compare August 8, 2024 01:03
@liamnaddell liamnaddell force-pushed the option_env branch 2 times, most recently from 74ad9b9 to 1f41fa7 Compare August 15, 2024 22:01
@liamnaddell
Copy link
Contributor Author

@CohenArthur Should I update this PR to use the lang-item paths from #3068 ?

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

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/compile/macros/builtin/option_env3.rs:
	Add second failure case for option_env
	* rust/execute/torture/builtin_macro_option_env.rs:
	Add execution case for option_env

Signed-off-by: Liam Naddell <[email protected]>
@P-E-P
Copy link
Member

P-E-P commented Sep 10, 2024

@CohenArthur Should I update this PR to use the lang-item paths from #3068 ?

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 core module where it should be the core library (aka technically the file itself since we're defining the builtin macro from within the crate).

@liamnaddell
Copy link
Contributor Author

@CohenArthur Should I update this PR to use the lang-item paths from #3068 ?

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 core module where it should be the core library (aka technically the file itself since we're defining the builtin macro from within the crate).

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.

@liamnaddell liamnaddell marked this pull request as draft September 10, 2024 15:28
@liamnaddell
Copy link
Contributor Author

Converting to a Draft PR until we have lang items integrated.

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

Successfully merging this pull request may close these issues.

Implement eager expansion for option_env!()
3 participants