-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Rewrite core::int, etc. to use item macros. Remove #[merge] hack #4219
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I'm interested in taking this on as my first Rust bug, but I need to mess around with modules and item macros a bit more to understand this specific scenario. |
Good luck! I myself am not too familiar with macros, but I have faith that it will work. |
Re implementation: I can't seem to think of a solution without cluttering Is this expected or is there a different approach here that I'm missing? |
@isaacaggrey Here's the pattern the compiler uses for importing macros: https://github.com/mozilla/rust/blob/incoming/src/librustc/middle/typeck/infer/lub.rs#L20 Does that work for you? |
Oh, you said it's the type-specific parts that are going to cause clutter. How bad is it? |
The type-specific bits are mostly pretty small. I don't have a great solution for putting that stuff in another file, although we could have some variant of
Could also use reexports:
That solution would not work well with rustdoc - not sure what it would do, but it wouldn't document the modules as |
I committed an XY Problem. What I meant about clutter was the naive solution of moving int-template.rs into core.rc as a macro and declaring each type module within core.rc since I didn't know it was possible to import macros (which is what I really wanted to do). In any case, the include! pattern is exactly the push I needed! Thanks, @brson! |
I had a working patch on master, but upon grabbing incoming I hit resolve errors that I assume are due to the crate-relative Implementation// core.rc
...
// FIXME(#3114): Macro import/export.
fn macros() { include!("integer-templates.rs"); }
/* Primitive types */
// pub mod i8 { int_template!(i8, 8) } // what it'll look like w/o temporary workaround below
pub mod i8 {
int_template!(i8, 8) // accepts type and number of bits
pub use i8::inst::*;
}
... // integer-templates.rs
{
macro_rules! int_template (
($typ:ty, $bits:expr) => {
pub mod inst { // FIXME(#3086): Macro expansion to multiple items
use char;
use cmp;
use cmp::{Eq, Ord};
use from_str::FromStr;
use iter;
use num;
use num::Num::from_int;
use str;
use uint;
use vec;
pub type T = $typ;
pub const bits : uint = $bits;
pub const bytes : uint = (bits / 8);
// rest of int-template.rs code
}
}
)
macro_rules! uint_template (
// etc
)
} I re-export at the end of each module to work around the workaround for multiple item expansion (extra module Aside: Another macro could be created to encapsulate what's common to both macros, but I'm not sure if that helps or hurts readability (yo dawg, macro in a macro...). IssuesThe compiler complains about not resolving the additional use declarations added in #4174 ( I tried placing the use declarations with the module body in core.rc, and I got a different set of resolve errors (e.g., ::i8::to_str). I imagine there might be some How can I modify the use declarations and/or re-export correctly to solve these issues? (Happy New Year and sorry for the length; feel free to refer me to someone else if necessary!) |
Not critical for 0.6 -- de-milestoning |
This should be more than possible after the next snapshot. Currently the stage0 compiler expands macro_rules! a(
() => (pub mod a {
#[cfg(a)] pub fn foo() {}
#[cfg(b)] pub fn foo() {}
})
)
a!()
fn main() { a::foo(); } to the This is a problem because a few methods in the templates (the iterators) are |
@alexcrichton Is it not the case that issues #4375 and #3114 are still blocking this issue from being fixed in a clean way? I don't think cfg attributes help here, but I very well may be misunderstanding your example. |
Well in the ideal world both those issues can be resolved, but I think that the main purpose of this bug is to remove the I started working on this before I realized it was already taken. You can use the I might take a stab at 4375 in the meantime, but I'll let you take over from here! |
@alexcrichton If you already have working code that fixes this issue, then by all means submit the pull request! I'm not self-centered enough to block working code from improving a project I'm interested in. :-) I'll just have to find a different first bug to work on. In hindsight, I should have requested that brson assign the issue to me since I suppose the comment I made or the work I attempted to show is not "official".
|
Oh my code isn't working at all, I stopped once I hit the |
Oh, I see. You're further along than I am, since my version of the code is back when the crate-relative Also, I didn't see your last line in your other comment, thank you. In that case, I'll ask brson to assign me to the bug in order to avoid further confusion. :-)
|
github does not allow assigning bugs to users who are not formally part of the project's team, unfortunately. |
Ah, I didn't know that! Well, I'm counting on submitting a pull request with working code soon to avoid further confusion. |
Use `size_of` from the prelude instead of imported
The core integer modules are all built from the same template files using a parser hack to merge in the type-specific portions. Now that we have item macros these could be expressed with
macro_rules! int_template { ... }
. Remove the#[merge]
attribute.The text was updated successfully, but these errors were encountered: