about summary refs log tree commit diff
path: root/library/proc_macro/src/lib.rs
AgeCommit message (Collapse)AuthorLines
2023-12-11Add support for `--env` on `tracked_env::var`Guillaume Gomez-1/+2
2023-12-11Improve `print_tts` by changing `tokenstream::Spacing`.Nicholas Nethercote-14/+12
`tokenstream::Spacing` appears on all `TokenTree::Token` instances, both punct and non-punct. Its current usage: - `Joint` means "can join with the next token *and* that token is a punct". - `Alone` means "cannot join with the next token *or* can join with the next token but that token is not a punct". The fact that `Alone` is used for two different cases is awkward. This commit augments `tokenstream::Spacing` with a new variant `JointHidden`, resulting in: - `Joint` means "can join with the next token *and* that token is a punct". - `JointHidden` means "can join with the next token *and* that token is a not a punct". - `Alone` means "cannot join with the next token". This *drastically* improves the output of `print_tts`. For example, this: ``` stringify!(let a: Vec<u32> = vec![];) ``` currently produces this string: ``` let a : Vec < u32 > = vec! [] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![] ; ``` (The space after the `]` is because `TokenTree::Delimited` currently doesn't have spacing information. The subsequent commit fixes this.) The new `print_tts` doesn't replicate original code perfectly. E.g. multiple space characters will be condensed into a single space character. But it's much improved. `print_tts` still produces the old, uglier output for code produced by proc macros. Because we have to translate the generated code from `proc_macro::Spacing` to the more expressive `token::Spacing`, which results in too much `proc_macro::Along` usage and no `proc_macro::JointHidden` usage. So `space_between` still exists and is used by `print_tts` in conjunction with the `Spacing` field. This change will also help with the removal of `Token::Interpolated`. Currently interpolated tokens are pretty-printed nicely via AST pretty printing. `Token::Interpolated` removal will mean they get printed with `print_tts`. Without this change, that would result in much uglier output for code produced by decl macro expansions. With this change, AST pretty printing and `print_tts` produce similar results. The commit also tweaks the comments on `proc_macro::Spacing`. In particular, it refers to "compound tokens" rather than "multi-char operators" because lifetimes aren't operators.
2023-11-15Bump cfg(bootstrap)sMark Rousskov-2/+2
2023-10-08rustdoc: remove rust logo from non-Rust cratesMichael Howell-0/+2
2023-09-26Auto merge of #116124 - WaffleLapkin:fix-proc-macro-literal-to-string, ↵bors-1/+9
r=compiler-errors Properly print cstr literals in `proc_macro::Literal::to_string` Previously we printed the contents of the string, rather than the actual string literal (e.g. `the c string` instead of `c"the c string"`). Fixes #112820 cc #105723
2023-09-24Fix `.to_string()` of `proc_macro::Literal` (properly print cstr literals)Maybe Waffle-1/+9
2023-09-23implement Literal::byte_characterEmil Gardström-0/+7
without this, the only way to create a `LitKind::Byte` is by doing `"b'a'".parse::<Literal>()`, this solves that by enabling `Literal::byte_character(b'a')`
2023-08-23Bump cfg(bootstrap)Mark Rousskov-1/+1
2023-08-16proc_macro: Update docs for `Spacing`Vadim Petrochenkov-13/+25
Brings the docs more in line with reality
2023-08-10Stabilize thread local cell methods.Mara Bos-1/+0
2023-08-03Add `internal_features` lintNilstrieb-0/+1
It lints against features that are inteded to be internal to the compiler and standard library. Implements MCP #596. We allow `internal_features` in the standard library and compiler as those use many features and this _is_ the standard library from the "internal to the compiler and standard library" after all. Marking some features as internal wasn't exactly the most scientific approach, I just marked some mostly obvious features. While there is a categorization in the macro, it's not very well upheld (should probably be fixed in another PR). We always pass `-Ainternal_features` in the testsuite About 400 UI tests and several other tests use internal features. Instead of throwing the attribute on each one, just always allow them. There's nothing wrong with testing internal features^^
2023-07-24Hide `ToString` implementations that specialize the default onesFrank Steffahn-2/+6
The status quo is highly confusing, since the overlap is not apparent, and specialization is not a feature of Rust. This addresses #87545; I'm not certain if it closes it, since that issue might also be trackign a *general* solution for hiding specializing impls automatically.
2023-06-20Merge proc_macro_span_shrink and proc_macro_spanJacob Pratt-2/+2
2023-06-20Add `Span::{line, column}`Jacob Pratt-0/+16
2023-06-20`Span::{before, after}` → `Span::{start, end}`Jacob Pratt-4/+4
2023-06-20Remove `LineColumn`, `Span::start`, `Span::end`Jacob Pratt-51/+0
2023-04-21Auto merge of #109002 - michaelvanstraten:master, r=petrochenkovbors-1/+7
Added byte position range for `proc_macro::Span` Currently, the [`Debug`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#impl-Debug-for-Span) implementation for [`proc_macro::Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#) calls the debug function implemented in the trait implementation of `server::Span` for the type `Rustc` in the `rustc-expand` crate. The current implementation, of the referenced function, looks something like this: ```rust fn debug(&mut self, span: Self::Span) -> String { if self.ecx.ecfg.span_debug { format!("{:?}", span) } else { format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0) } } ``` It returns the byte position of the [`Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#) as an interpolated string. Because this is currently the only way to get a spans position in the file, I might lead someone, who is interested in this information, to parsing this interpolated string back into a range of bytes, which I think is a very non-rusty way. The proposed `position()`, method implemented in this PR, gives the ability to directly get this info. It returns a [`std::ops::Range`](https://doc.rust-lang.org/std/ops/struct.Range.html#) wrapping the lowest and highest byte of the [`Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#). I put it behind the `proc_macro_span` feature flag because many of the other functions that have a similar footprint also are annotated with it, I don't actually know if this is right. It would be great if somebody could take a look at this, thank you very much in advanced.
2023-04-12remove some unneeded importsKaDiWa-3/+3
2023-03-11Renamed to byte_range and changed Range generics [skip ci]Michael van Straten-2/+2
2023-03-10added byte position range for proc_macro::SpanMichael van Straten-1/+1
2023-03-10added byte position range for proc_macro::SpanMichael van Straten-1/+7
2023-01-26Auto merge of #107318 - matthiaskrgr:rollup-776kd81, r=matthiaskrgrbors-1/+1
Rollup of 9 pull requests Successful merges: - #97373 (impl DispatchFromDyn for Cell and UnsafeCell) - #106625 (Remove backwards compat for LLVM 12 coverage format) - #106779 (Avoid __cxa_thread_atexit_impl on Emscripten) - #106811 (Append .dwp to the binary filename instead of replacing the existing extension.) - #106836 (Remove optimistic spinning from `mpsc::SyncSender`) - #106946 (implement Hash for proc_macro::LineColumn) - #107074 (remove unnecessary check for opaque types) - #107287 (Improve fn pointer notes) - #107304 (Use `can_eq` to compare types for default assoc type error) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2023-01-16implement Hash for proc_macro::LineColumnDavid Tolnay-1/+1
2023-01-12Improve proc macro attribute diagnosticsmejrs-0/+1
2022-12-12Add a "the" to proc_macro documentationest31-1/+1
2022-11-06Bump version placeholders to releaseMark Rousskov-1/+1
2022-10-18Stabilize proc_macro::Span::source_textest31-1/+1
Splits proc_macro::Span::source_text into a new feature gate and stabilizes it.
2022-09-17Fix typo in proc_macro Span::eqjam1garner-1/+1
2022-08-19use <[u8]>::escape_ascii instead of core::ascii::escape_defaultKaDiWa-6/+1
2022-07-24Expose size_hint() for TokenStream's iteratorDavid Tolnay-0/+8
2022-07-20Rollup merge of #99516 - m-ou-se:proc-macro-tracked-tracking-issue, ↵Matthias Krüger-4/+4
r=Mark-Simulacrum Use new tracking issue for proc_macro::tracked_*.
2022-07-20Use new tracking issue for proc_macro::tracked_*.Mara Bos-4/+4
2022-07-18proc_macro: Move subspan to be a method on Span in the bridgeNika Layzell-6/+1
This method is still only used for Literal::subspan, however the implementation only depends on the Span component, so it is simpler and more efficient for now to pass down only the information that is needed. In the future, if more information about the Literal is required in the implementation (e.g. to validate that spans line up as expected with source text), that extra information can be added back with extra arguments.
2022-07-18proc_macro: stop using a remote object handle for LiteralNika Layzell-28/+110
This builds on the symbol infrastructure built for `Ident` to replicate the `LitKind` and `Lit` structures in rustc within the `proc_macro` client, allowing literals to be fully created and interacted with from the client thread. Only parsing and subspan operations still require sync RPC.
2022-07-18proc_macro: stop using a remote object handle for IdentNika Layzell-16/+31
Doing this for all unicode identifiers would require a dependency on `unicode-normalization` and `rustc_lexer`, which is currently not possible for `proc_macro` due to it being built concurrently with `std` and `core`. Instead, ASCII identifiers are validated locally, and an RPC message is used to validate unicode identifiers when needed. String values are interned on the both the server and client when deserializing, to avoid unnecessary copies and keep Ident cheap to copy and move. This appears to be important for performance. The client-side interner is based roughly on the one from rustc_span, and uses an arena inspired by rustc_arena. RPC messages passing symbols always include the full value. This could potentially be optimized in the future if it is revealed to be a performance bottleneck. Despite now having a relevant implementaion of Display for Ident, ToString is still specialized, as it is a hot-path for this object. The symbol infrastructure will also be used for literals in the next part.
2022-07-18proc_macro: Specialize Punct::to_stringNika Layzell-0/+7
This was removed in a previous part, however it should be specialized for to_string performance and consistency.
2022-06-28review changesNika Layzell-2/+6
longer names for RPC generics and reduced dependency on macros in the server.
2022-06-26proc_macro: stop using a remote object handle for GroupNika Layzell-11/+15
This greatly reduces round-trips to fetch relevant extra information about the token in proc macro code, and avoids RPC messages to create Group tokens.
2022-06-26proc_macro: stop using a remote object handle for PunctNika Layzell-19/+17
This greatly reduces round-trips to fetch relevant extra information about the token in proc macro code, and avoids RPC messages to create Punct tokens.
2022-06-25proc_macro: cache static spans in client's thread-local stateNika Layzell-1/+1
This greatly improves the performance of the very frequently called `call_site()` macro when running in a cross-thread configuration.
2022-06-17review fixupsNika Layzell-15/+15
2022-06-17Try to reduce codegen complexity of TokenStream's FromIterator and Extend implsNika Layzell-16/+92
This is an experimental patch to try to reduce the codegen complexity of TokenStream's FromIterator and Extend implementations for downstream crates, by moving the core logic into a helper type. This might help improve build performance of crates which depend on proc_macro as iterators are used less, and the compiler may take less time to do things like attempt specializations or other iterator optimizations. The change intentionally sacrifices some optimization opportunities, such as using the specializations for collecting iterators derived from Vec::into_iter() into Vec. This is one of the simpler potential approaches to reducing the amount of code generated in crates depending on proc_macro, so it seems worth trying before other more-involved changes.
2022-06-17proc_macro: reduce the number of messages required to create, extend, and ↵Nika Layzell-24/+54
iterate TokenStreams This significantly reduces the cost of common interactions with TokenStream when running with the CrossThread execution strategy, by reducing the number of RPC calls required.
2022-06-03Fully stabilize NLLJack Huey-1/+0
2022-06-02Revert #96682.Nicholas Nethercote-3/+2
The change was "Show invisible delimiters (within comments) when pretty printing". It's useful to show these delimiters, but is a breaking change for some proc macros. Fixes #97608.
2022-05-04Show invisible delimeters (within comments) when pretty printing.Nicholas Nethercote-2/+3
2022-04-28rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter`Vadim Petrochenkov-2/+2
2022-04-09Rollup merge of #95308 - bjorn3:more_stable_proc_macro, r=Mark-SimulacrumDylan DPC-3/+3
Reduce the amount of unstable features used in libproc_macro This makes it easier to adapt the source for stable when copying it into rust-analyzer to load rustc compiled proc macros.
2022-04-05trivial cfg(bootstrap) changesPietro Albini-2/+0
2022-03-25Add note about feature gatesbjorn3-0/+3