Skip to content

Re-write locale macros as const functions #348

Closed
@sffc

Description

@sffc

Proc macros are clunky and error-prone. Problems include:

  1. You cannot depend on icu_locid_macros by itself; you need to include either icu or icu_locid
  2. They are heavier than macro_rules! and const fn, since the compiler needs to link and run them as a separate binary
  3. The macros crate needs to be published separately on crates.io
  4. Pulls in a nontrivial proc-macro-crate dependency in Cargo.toml

We are close to being able to write the locale macros as const functions.

I previous said in #310 (comment):

Okay, I also do not see any sign of string/slice indexing operations. I saw it suggested in rust-lang/rust#52000 that this could change (allowing for loops), but there doesn't seem to be a lot of movement on the idea of the fundamental indexing operation in const functions.

That being said, since we can pretty trivially implement things like TinyStr4::is_ascii_alphabetic() as const functions, we could limit the locale macro to be along the lines of

langid!("en")
langid!("en", "US")
langid!("en", "Latn", "US")

We can validate the subtags, but we can't parse the "en-Latn-US" syntax inside the const function in any clean way. (I guess you could convert the string to a TinyStr16 and do bitwise operations on that...)

I think we should consider this route as a more future-proof solution to this problem.

Metadata

Metadata

Assignees

Labels

C-localeComponent: Locale identifiers, BCP47T-techdebtType: ICU4X code health and tech debtgood first issueGood for newcomers

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions