Skip to content

Commit 32dbcf3

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 6288b6f commit 32dbcf3

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
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: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use rustc_ast::token;
2222
use rustc_ast::tokenstream::TokenStream;
2323
use rustc_ast_pretty::pprust;
2424
use rustc_errors::{Diag, EmissionGuarantee, FatalError, PResult, pluralize};
25+
pub use rustc_lexer::UNICODE_VERSION;
2526
use rustc_session::parse::ParseSess;
2627
use rustc_span::source_map::SourceMap;
2728
use rustc_span::{FileName, SourceFile, Span};
28-
pub use unicode_normalization::UNICODE_VERSION as UNICODE_NORMALIZATION_VERSION;
2929

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

@@ -39,6 +39,44 @@ 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 rustc_lexer = rustc_lexer::UNICODE_VERSION;
45+
let rustc_span = rustc_span::UNICODE_VERSION;
46+
let normalization = unicode_normalization::UNICODE_VERSION;
47+
let width = unicode_width::UNICODE_VERSION;
48+
49+
if rustc_lexer.0 != rustc_span.0
50+
|| rustc_lexer.1 != rustc_span.1
51+
|| rustc_lexer.2 != rustc_span.2
52+
{
53+
panic!(
54+
"rustc_lexer and rustc_span must use the same Unicode version, \
55+
`rustc_lexer::UNICODE_VERSION` and `rustc_span::UNICODE_VERSION` are \
56+
different."
57+
);
58+
}
59+
60+
if rustc_lexer.0 != normalization.0
61+
|| rustc_lexer.1 != normalization.1
62+
|| rustc_lexer.2 != normalization.2
63+
{
64+
panic!(
65+
"rustc_lexer and unicode-normalization must use the same Unicode version, \
66+
`rustc_lexer::UNICODE_VERSION` and `unicode_normalization::UNICODE_VERSION` are \
67+
different."
68+
);
69+
}
70+
71+
if rustc_lexer.0 != width.0 || rustc_lexer.1 != width.1 || rustc_lexer.2 != width.2 {
72+
panic!(
73+
"rustc_lexer and unicode-width must use the same Unicode version, \
74+
`rustc_lexer::UNICODE_VERSION` and `unicode_width::UNICODE_VERSION` are \
75+
different."
76+
);
77+
}
78+
};
79+
4280
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
4381

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

compiler/rustc_span/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3939
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
4040
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4141
use tracing::debug;
42+
pub use unicode_width::UNICODE_VERSION;
4243

4344
mod caching_source_map_view;
4445
pub mod source_map;

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![feature(rustc_private)]
1313

1414
extern crate rustc_driver;
15-
extern crate rustc_lexer;
1615
extern crate rustc_parse;
1716

1817
fn main() {
@@ -22,9 +21,5 @@ fn main() {
2221
it should also be updated in the reference at \
2322
https://github.com/rust-lang/reference/blob/HEAD/src/identifiers.md."
2423
);
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-
);
24+
println!("Unicode version used in rustc_parse is: {:?}", rustc_parse::UNICODE_VERSION);
3025
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
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_parse is: (17, 0, 0)

0 commit comments

Comments
 (0)