Code Monkey home page Code Monkey logo

weird-proc-macro-spans's Introduction

Spans in procedural macros

The motivation for creating this README and repo was originally from the gnome-class project. This project is defining a procedural macro with a small DSL to conveniently declare glib-compatible types and interface to them from both Rust and C. The crate is currently (as of this writing) undergoing a rewrite to transition to a "new" procedural macro which preserves span information to ensure that type errors and such in user-written code are legible.

So what I did was enable the unstable feature of the proc-macro2 crate (which forces it to implement itself with the upstream proc_macro crate) and then oh boy down the rabbit hole I went...

Organization

This project has a few pieces:

  • src/lib.rs - this is all the procedural macros for all the tests. There's some more comments about the file internally.
  • tests/{failure,success}N.rs - these are a bunch of files showing off whether compilation fails or succeeds. For example failure1.rs is an example of a procedural macro invocation that fails to compile, while success1.rs is a small tweak on behalf of the procedural macro author's side of things which gets the procedural macro to succeed to compile.
  • foo - a helper crate for some of the test cases.

For example here's what happened with the failure2 test:

$ cargo +nightly test --test failure2
   Compiling wut v0.1.0 (file:///Users/acrichton/code/weird-proc-macro-spans)
error[E0432]: unresolved import `super`
 --> tests/failure2.rs:8:1
  |
8 | / failure2! {
9 | | }
  | |_^ no `A` in the root

error: aborting due to previous error

error: Could not compile `wut`.

This was an example, if we look at the procedural macro definition, of how a generated module can't import contents defined outside. We can see the fix, however:

$ cargo +nightly test --test success2
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/success2-c6ae2bc09f230207

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

One possible fix in this case, looking at the definition of success2, was to switch all spans to call_site.

weird-proc-macro-spans's People

Contributors

alexcrichton avatar

Stargazers

Trevor avatar

Watchers

 avatar James Cloos avatar David Tolnay avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.