Skip to content

Add proc_macro::Literal::int_unsuffixed and proc_macro::Literal::float_unsuffixed #657

@nik-rev

Description

@nik-rev

Proposal

Problem statement

I made a crate culit that lets you have custom literals:

use culit::culit;
use std::num::NonZeroUsize;

#[culit]
fn main() {
    // custom literal, ends with "nzusize". Expand to `custom_literal::integer::nzusize!(100)`
    assert_eq!(100nzusize, NonZeroUsize::new(100).unwrap());
    // COMPILE ERROR!
    // let illegal = 0nzusize;
}

mod custom_literal {
    pub mod integer {
        macro_rules! nzusize {
            // handle `0` specially
            (0) => {
                compile_error!("`0` is not a valid `NonZeroUsize`")
            };
            ($value:literal) => {
                const { NonZeroUsize::new($value).unwrap() }
            };
        }
        pub(crate) use nzusize;
    }
}

100nzusize expands to crate::custom_literal::integer::nzusize!(100). it works like this:

  • Inside the proc_macro, I obtain the string "100" then parse it as a u128, then pass it to proc_macro::Literal::u128_unsuffixed to generate the Rust literal 100 which is directly passed to nzusize! macro

The problem is that the Literal API does not allow me to create a literal of a completely arbitrary size. The maximum value a literal created from a proc macro can be is u128::MAX, but literals that are inputs to the proc macro can be of arbitrary length

Motivating examples or use cases

Suppose a user of my crate wanted to make a "bigint" literal that can be of arbitrary size, well they can't! Their big integers will be restricted to u128::MAX, because proc macros cannot create literals that are larger than u128::MAX

Solution sketch

I propose to add new functions to proc_macro::Literal:

impl Literal {
    fn int_unsuffixed(int: &str) -> Literal { /* ... */ }
    fn float_unsuffixed(float: &str) -> Literal { /* ... */ }
}

This directly creates the Literal without any restrictions on size, so it is in a string. The integer is still validated to be a valid float/integer (and panics if it isn't), but its length can be unlimited

I think we should call them _unsuffixed to allow for the possibility of int_suffixed and float_suffixed which would accept arbitrary suffix &str. This could be useful if another crate generated code that my proc macro consumed, but it is not part of this proposal

Alternatives

Limit ourselves to emitting literals that are at most u128 big. So the APIs people can create are less powerful

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions