Skip to content

Commit a8d4e4f

Browse files
authored
Rollup merge of rust-lang#62848 - matklad:xid-unicode, r=petrochenkov
Use unicode-xid crate instead of libcore This PR proposes to remove `char::is_xid_start` and `char::is_xid_continue` functions from `libcore` and use `unicode_xid` crate from crates.io (note that this crate is already present in rust-lang/rust's Cargo.lock). Reasons to do this: * removing rustc-binary-specific stuff from libcore * making sure that, across the ecosystem, there's a single definition of what rust identifier is (`unicode-xid` has almost 10 million downs, as a `proc_macro2` dependency) * making it easier to share `rustc_lexer` crate with rust-analyzer: no need to `#[cfg]` if we are building as a part of the compiler Reasons not to do this: * increased maintenance burden: we'll need to upgrade unicode version both in libcore and in unicode-xid. However, this shouldn't be a too heavy burden: just running `./unicode.py` after new unicode version. I (@matklad) am ready to be a t-compiler side maintainer of unicode-xid. Moreover, given that xid-unicode is an important dependency of syn, *someone* needs to maintain it anyway. * xid-unicode implementation is significantly slower. It uses a more compact table with binary search, instead of a trie. However, this shouldn't matter in practice, because we have fast-path for ascii anyway, and code size savings is a plus. Moreover, in rust-lang#59706 not using libcore turned out to be *faster*, presumably beacause checking for whitespace with match is even faster. <details> <summary>old description</summary> Followup to rust-lang#59706 r? @eddyb Note that this doesn't actually remove tables from libcore, to avoid conflict with rust-lang#62641. cc unicode-rs/unicode-xid#11 </details>
2 parents a24f636 + 206fe8e commit a8d4e4f

File tree

17 files changed

+111
-510
lines changed

17 files changed

+111
-510
lines changed

Cargo.lock

+13-4
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ dependencies = [
10111011
name = "fmt_macros"
10121012
version = "0.0.0"
10131013
dependencies = [
1014+
"rustc_lexer",
10141015
"syntax_pos",
10151016
]
10161017

@@ -2372,7 +2373,7 @@ version = "0.4.30"
23722373
source = "registry+https://github.com/rust-lang/crates.io-index"
23732374
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
23742375
dependencies = [
2375-
"unicode-xid",
2376+
"unicode-xid 0.1.0",
23762377
]
23772378

23782379
[[package]]
@@ -3290,7 +3291,7 @@ dependencies = [
32903291
name = "rustc_lexer"
32913292
version = "0.1.0"
32923293
dependencies = [
3293-
"unicode-xid",
3294+
"unicode-xid 0.2.0",
32943295
]
32953296

32963297
[[package]]
@@ -3368,6 +3369,7 @@ dependencies = [
33683369
"rustc_apfloat",
33693370
"rustc_data_structures",
33703371
"rustc_errors",
3372+
"rustc_lexer",
33713373
"rustc_target",
33723374
"serialize",
33733375
"smallvec",
@@ -3976,7 +3978,7 @@ checksum = "641e117d55514d6d918490e47102f7e08d096fdde360247e4a10f7a91a8478d3"
39763978
dependencies = [
39773979
"proc-macro2",
39783980
"quote",
3979-
"unicode-xid",
3981+
"unicode-xid 0.1.0",
39803982
]
39813983

39823984
[[package]]
@@ -3988,7 +3990,7 @@ dependencies = [
39883990
"proc-macro2",
39893991
"quote",
39903992
"syn",
3991-
"unicode-xid",
3993+
"unicode-xid 0.1.0",
39923994
]
39933995

39943996
[[package]]
@@ -4017,6 +4019,7 @@ dependencies = [
40174019
"log",
40184020
"rustc_data_structures",
40194021
"rustc_errors",
4022+
"rustc_lexer",
40204023
"rustc_target",
40214024
"smallvec",
40224025
"syntax",
@@ -4532,6 +4535,12 @@ version = "0.1.0"
45324535
source = "registry+https://github.com/rust-lang/crates.io-index"
45334536
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
45344537

4538+
[[package]]
4539+
name = "unicode-xid"
4540+
version = "0.2.0"
4541+
source = "registry+https://github.com/rust-lang/crates.io-index"
4542+
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
4543+
45354544
[[package]]
45364545
name = "unicode_categories"
45374546
version = "0.1.1"

src/libcore/char/methods.rs

-23
Original file line numberDiff line numberDiff line change
@@ -547,29 +547,6 @@ impl char {
547547
}
548548
}
549549

550-
/// Returns `true` if this `char` satisfies the `XID_Start` Unicode property, and false
551-
/// otherwise.
552-
///
553-
/// `XID_Start` is a Unicode Derived Property specified in
554-
/// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
555-
/// mostly similar to `ID_Start` but modified for closure under `NFKx`.
556-
#[unstable(feature = "unicode_internals", issue = "0")]
557-
pub fn is_xid_start(self) -> bool {
558-
derived_property::XID_Start(self)
559-
}
560-
561-
/// Returns `true` if this `char` satisfies the `XID_Continue` Unicode property, and false
562-
/// otherwise.
563-
///
564-
/// `XID_Continue` is a Unicode Derived Property specified in
565-
/// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
566-
/// mostly similar to `ID_Continue` but modified for closure under NFKx.
567-
#[unstable(feature = "unicode_internals", issue = "0")]
568-
#[inline]
569-
pub fn is_xid_continue(self) -> bool {
570-
derived_property::XID_Continue(self)
571-
}
572-
573550
/// Returns `true` if this `char` is lowercase.
574551
///
575552
/// 'Lowercase' is defined according to the terms of the Unicode Derived Core

src/libcore/unicode/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,3 @@ pub mod derived_property {
1313
pub mod conversions {
1414
pub use crate::unicode::tables::conversions::{to_lower, to_upper};
1515
}
16-
17-
// For use in libsyntax
18-
pub mod property {
19-
pub use crate::unicode::tables::property::Pattern_White_Space;
20-
}

src/libcore/unicode/tables.rs

-375
Large diffs are not rendered by default.

src/libcore/unicode/unicode.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def generate_property_module(mod, grouped_categories, category_subset):
728728

729729
yield "pub(crate) mod %s {\n" % mod
730730
for cat in sorted(category_subset):
731-
if cat in ("Cc", "White_Space", "Pattern_White_Space"):
731+
if cat in ("Cc", "White_Space"):
732732
generator = generate_small_bool_trie("%s_table" % cat, grouped_categories[cat])
733733
else:
734734
generator = generate_bool_trie("%s_table" % cat, grouped_categories[cat])
@@ -841,19 +841,18 @@ def main():
841841
unicode_data = load_unicode_data(get_path(UnicodeFiles.UNICODE_DATA))
842842
load_special_casing(get_path(UnicodeFiles.SPECIAL_CASING), unicode_data)
843843

844-
want_derived = {"XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase",
844+
want_derived = {"Alphabetic", "Lowercase", "Uppercase",
845845
"Cased", "Case_Ignorable", "Grapheme_Extend"}
846846
derived = load_properties(get_path(UnicodeFiles.DERIVED_CORE_PROPERTIES), want_derived)
847847

848848
props = load_properties(get_path(UnicodeFiles.PROPS),
849-
{"White_Space", "Join_Control", "Noncharacter_Code_Point",
850-
"Pattern_White_Space"})
849+
{"White_Space", "Join_Control", "Noncharacter_Code_Point"})
851850

852851
# Category tables
853852
for (name, categories, category_subset) in (
854853
("general_category", unicode_data.general_categories, ["N", "Cc"]),
855854
("derived_property", derived, want_derived),
856-
("property", props, ["White_Space", "Pattern_White_Space"])
855+
("property", props, ["White_Space"])
857856
):
858857
for fragment in generate_property_module(name, categories, category_subset):
859858
buf.write(fragment)

src/libfmt_macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ path = "lib.rs"
1010

1111
[dependencies]
1212
syntax_pos = { path = "../libsyntax_pos" }
13-
13+
rustc_lexer = { path = "../librustc_lexer" }

src/libfmt_macros/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,11 @@ impl<'a> Parser<'a> {
597597
}
598598
}
599599

600-
/// Parses a word starting at the current position. A word is considered to
601-
/// be an alphabetic character followed by any number of alphanumeric
602-
/// characters.
600+
/// Parses a word starting at the current position. A word is the same as
601+
/// Rust identifier, except that it can't start with `_` character.
603602
fn word(&mut self) -> &'a str {
604603
let start = match self.cur.peek() {
605-
Some(&(pos, c)) if c.is_xid_start() => {
604+
Some(&(pos, c)) if c != '_' && rustc_lexer::is_id_start(c) => {
606605
self.cur.next();
607606
pos
608607
}
@@ -611,7 +610,7 @@ impl<'a> Parser<'a> {
611610
}
612611
};
613612
while let Some(&(pos, c)) = self.cur.peek() {
614-
if c.is_xid_continue() {
613+
if rustc_lexer::is_id_continue(c) {
615614
self.cur.next();
616615
} else {
617616
return &self.input[start..pos];

src/librustc_lexer/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ name = "rustc_lexer"
44
version = "0.1.0"
55
edition = "2018"
66

7-
# Note that this crate purposefully does not depend on other rustc crates
8-
[dependencies]
9-
unicode-xid = { version = "0.1.0", optional = true }
10-
117
# Note: do not remove this blank `[lib]` section.
128
# This will be used when publishing this crate as `rustc-ap-rustc_lexer`.
139
[lib]
1410
doctest = false
1511
name = "rustc_lexer"
12+
13+
# Note that this crate purposefully does not depend on other rustc crates
14+
[dependencies]
15+
unicode-xid = "0.2.0"

0 commit comments

Comments
 (0)