Skip to content

Commit 064ca73

Browse files
committed
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.
1 parent d9d302c commit 064ca73

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,26 @@ use LiteralKind::*;
3434
use TokenKind::*;
3535
use cursor::EOF_CHAR;
3636
pub use cursor::{Cursor, FrontmatterAllowed};
37-
pub use unicode_ident::UNICODE_VERSION as UNICODE_IDENT_VERSION;
37+
pub use unicode_ident::UNICODE_VERSION;
3838
use unicode_properties::UnicodeEmoji;
3939

40+
// Make sure that the Unicode version of the dependencies is the same.
41+
const _: () = {
42+
let properties = unicode_properties::UNICODE_VERSION;
43+
let ident = unicode_ident::UNICODE_VERSION;
44+
45+
if properties.0 != ident.0 as u64
46+
|| properties.1 != ident.1 as u64
47+
|| properties.2 != ident.2 as u64
48+
{
49+
panic!(
50+
"unicode-properties and unicode-ident must use the same Unicode version, \
51+
`unicode_properties::UNICODE_VERSION` and `unicode_ident::UNICODE_VERSION` are \
52+
different."
53+
);
54+
}
55+
};
56+
4057
/// Parsed token.
4158
/// It doesn't contain information about data that has been parsed,
4259
/// only the type of the token and its size.

compiler/rustc_parse/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_errors::{Diag, EmissionGuarantee, FatalError, PResult, pluralize};
2525
use rustc_session::parse::ParseSess;
2626
use rustc_span::source_map::SourceMap;
2727
use rustc_span::{FileName, SourceFile, Span};
28-
pub use unicode_normalization::UNICODE_VERSION as UNICODE_NORMALIZATION_VERSION;
28+
pub use unicode_normalization::UNICODE_VERSION;
2929

3030
pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
3131

@@ -39,6 +39,20 @@ pub mod lexer;
3939

4040
mod errors;
4141

42+
// Make sure that the Unicode version of the dependencies is the same.
43+
const _: () = {
44+
let normalization = unicode_normalization::UNICODE_VERSION;
45+
let width = unicode_width::UNICODE_VERSION;
46+
47+
if normalization.0 != width.0 || normalization.1 != width.1 || normalization.2 != width.2 {
48+
panic!(
49+
"unicode-normalization and unicode-width must use the same Unicode version, \
50+
`unicode_normalization::UNICODE_VERSION` and `unicode_width::UNICODE_VERSION` are \
51+
different."
52+
);
53+
}
54+
};
55+
4256
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
4357

4458
// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.

tests/ui-fulldeps/lexer/unicode-version.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ fn main() {
2222
it should also be updated in the reference at \
2323
https://github.com/rust-lang/reference/blob/HEAD/src/identifiers.md."
2424
);
25-
println!("Unicode version of unicode-ident is: {:?}", rustc_lexer::UNICODE_IDENT_VERSION);
26-
println!(
27-
"Unicode version of unicode-normalization is: {:?}",
28-
rustc_parse::UNICODE_NORMALIZATION_VERSION
29-
);
25+
println!("Unicode version used in rustc_lexer is: {:?}", rustc_lexer::UNICODE_VERSION);
26+
println!("Unicode version used in rustc_parse is: {:?}", rustc_parse::UNICODE_VERSION);
3027
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Checking if Unicode version changed.
22
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.
3-
Unicode version of unicode-ident is: (17, 0, 0)
4-
Unicode version of unicode-normalization is: (17, 0, 0)
3+
Unicode version used in rustc_lexer is: (17, 0, 0)
4+
Unicode version used in rustc_parse is: (17, 0, 0)

0 commit comments

Comments
 (0)