lexer/parser: ensure deps use the same unicode version

Add a compile time check in rustc_lexer and rustc_parse ensuring that unicode-related dependencies within the crate use the same unicode version.
These checks are inspired by the examples privided by @clarfonthey.
This commit is contained in:
Marcondiro
2025-12-13 12:21:00 +01:00
parent ca64688b37
commit f7cb82e70a
5 changed files with 60 additions and 10 deletions
+18 -1
View File
@@ -34,9 +34,26 @@
use TokenKind::*;
use cursor::EOF_CHAR;
pub use cursor::{Cursor, FrontmatterAllowed};
pub use unicode_ident::UNICODE_VERSION as UNICODE_IDENT_VERSION;
pub use unicode_ident::UNICODE_VERSION;
use unicode_properties::UnicodeEmoji;
// Make sure that the Unicode version of the dependencies is the same.
const _: () = {
let properties = unicode_properties::UNICODE_VERSION;
let ident = unicode_ident::UNICODE_VERSION;
if properties.0 != ident.0 as u64
|| properties.1 != ident.1 as u64
|| properties.2 != ident.2 as u64
{
panic!(
"unicode-properties and unicode-ident must use the same Unicode version, \
`unicode_properties::UNICODE_VERSION` and `unicode_ident::UNICODE_VERSION` are \
different."
);
}
};
/// Parsed token.
/// It doesn't contain information about data that has been parsed,
/// only the type of the token and its size.
+39 -1
View File
@@ -22,10 +22,10 @@
use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust;
use rustc_errors::{Diag, EmissionGuarantee, FatalError, PResult, pluralize};
pub use rustc_lexer::UNICODE_VERSION;
use rustc_session::parse::ParseSess;
use rustc_span::source_map::SourceMap;
use rustc_span::{FileName, SourceFile, Span};
pub use unicode_normalization::UNICODE_VERSION as UNICODE_NORMALIZATION_VERSION;
pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
@@ -39,6 +39,44 @@
mod errors;
// Make sure that the Unicode version of the dependencies is the same.
const _: () = {
let rustc_lexer = rustc_lexer::UNICODE_VERSION;
let rustc_span = rustc_span::UNICODE_VERSION;
let normalization = unicode_normalization::UNICODE_VERSION;
let width = unicode_width::UNICODE_VERSION;
if rustc_lexer.0 != rustc_span.0
|| rustc_lexer.1 != rustc_span.1
|| rustc_lexer.2 != rustc_span.2
{
panic!(
"rustc_lexer and rustc_span must use the same Unicode version, \
`rustc_lexer::UNICODE_VERSION` and `rustc_span::UNICODE_VERSION` are \
different."
);
}
if rustc_lexer.0 != normalization.0
|| rustc_lexer.1 != normalization.1
|| rustc_lexer.2 != normalization.2
{
panic!(
"rustc_lexer and unicode-normalization must use the same Unicode version, \
`rustc_lexer::UNICODE_VERSION` and `unicode_normalization::UNICODE_VERSION` are \
different."
);
}
if rustc_lexer.0 != width.0 || rustc_lexer.1 != width.1 || rustc_lexer.2 != width.2 {
panic!(
"rustc_lexer and unicode-width must use the same Unicode version, \
`rustc_lexer::UNICODE_VERSION` and `unicode_width::UNICODE_VERSION` are \
different."
);
}
};
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.
+1
View File
@@ -39,6 +39,7 @@
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use tracing::debug;
pub use unicode_width::UNICODE_VERSION;
mod caching_source_map_view;
pub mod source_map;
+1 -6
View File
@@ -12,7 +12,6 @@
#![feature(rustc_private)]
extern crate rustc_driver;
extern crate rustc_lexer;
extern crate rustc_parse;
fn main() {
@@ -22,9 +21,5 @@ fn main() {
it should also be updated in the reference at \
https://github.com/rust-lang/reference/blob/HEAD/src/identifiers.md."
);
println!("Unicode version of unicode-ident is: {:?}", rustc_lexer::UNICODE_IDENT_VERSION);
println!(
"Unicode version of unicode-normalization is: {:?}",
rustc_parse::UNICODE_NORMALIZATION_VERSION
);
println!("Unicode version used in rustc_parse is: {:?}", rustc_parse::UNICODE_VERSION);
}
@@ -1,4 +1,3 @@
Checking if Unicode version changed.
If the Unicode version changes are intentional, it should also be updated in the reference at https://github.com/rust-lang/reference/blob/HEAD/src/identifiers.md.
Unicode version of unicode-ident is: (17, 0, 0)
Unicode version of unicode-normalization is: (17, 0, 0)
Unicode version used in rustc_parse is: (17, 0, 0)