|
42 | 42 | #include "rust-early-name-resolver.h"
|
43 | 43 | #include "rust-cfg-strip.h"
|
44 | 44 | #include "rust-expand-visitor.h"
|
| 45 | +#include "rust-unicode.h" |
45 | 46 |
|
46 | 47 | #include "diagnostic.h"
|
47 | 48 | #include "input.h"
|
@@ -107,30 +108,39 @@ infer_crate_name (const std::string &filename)
|
107 | 108 | return crate;
|
108 | 109 | }
|
109 | 110 |
|
110 |
| -/* Validate the crate name using the ASCII rules |
111 |
| - TODO: Support Unicode version of the rules */ |
| 111 | +/* Validate the crate name using the ASCII rules */ |
112 | 112 |
|
113 | 113 | static bool
|
114 | 114 | validate_crate_name (const std::string &crate_name, Error &error)
|
115 | 115 | {
|
116 |
| - if (crate_name.empty ()) |
| 116 | + Utf8String utf8_name = {crate_name}; |
| 117 | + tl::optional<std::vector<Codepoint>> uchars_opt = utf8_name.get_chars (); |
| 118 | + |
| 119 | + if (!uchars_opt.has_value ()) |
| 120 | + { |
| 121 | + error = Error (UNDEF_LOCATION, "crate name is not a valid UTF-8 string"); |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + std::vector<Codepoint> uchars = uchars_opt.value (); |
| 126 | + if (uchars.empty ()) |
117 | 127 | {
|
118 | 128 | error = Error (UNDEF_LOCATION, "crate name cannot be empty");
|
119 | 129 | return false;
|
120 | 130 | }
|
121 |
| - if (crate_name.length () > kMaxNameLength) |
| 131 | + if (uchars.size () > kMaxNameLength) |
122 | 132 | {
|
123 | 133 | error = Error (UNDEF_LOCATION, "crate name cannot exceed %lu characters",
|
124 | 134 | (unsigned long) kMaxNameLength);
|
125 | 135 | return false;
|
126 | 136 | }
|
127 |
| - for (auto &c : crate_name) |
| 137 | + for (Codepoint &c : uchars) |
128 | 138 | {
|
129 |
| - if (!(ISALNUM (c) || c == '_')) |
| 139 | + if (!(is_alphabetic (c.value) || is_numeric (c.value) || c.value == '_')) |
130 | 140 | {
|
131 | 141 | error = Error (UNDEF_LOCATION,
|
132 |
| - "invalid character %<%c%> in crate name: %<%s%>", c, |
133 |
| - crate_name.c_str ()); |
| 142 | + "invalid character %<%s%> in crate name: %<%s%>", |
| 143 | + c.as_string ().c_str (), crate_name.c_str ()); |
134 | 144 | return false;
|
135 | 145 | }
|
136 | 146 | }
|
@@ -1273,13 +1283,17 @@ rust_crate_name_validation_test (void)
|
1273 | 1283 | ASSERT_TRUE (Rust::validate_crate_name ("example", error));
|
1274 | 1284 | ASSERT_TRUE (Rust::validate_crate_name ("abcdefg_1234", error));
|
1275 | 1285 | ASSERT_TRUE (Rust::validate_crate_name ("1", error));
|
1276 |
| - // FIXME: The next test does not pass as of current implementation |
1277 |
| - // ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓")); |
| 1286 | + ASSERT_TRUE (Rust::validate_crate_name ("クレート", error)); |
| 1287 | + ASSERT_TRUE (Rust::validate_crate_name ("Sōkrátēs", error)); |
| 1288 | + ASSERT_TRUE (Rust::validate_crate_name ("惊吓", error)); |
| 1289 | + |
1278 | 1290 | // NOTE: - is not allowed in the crate name ...
|
1279 | 1291 |
|
1280 | 1292 | ASSERT_FALSE (Rust::validate_crate_name ("abcdefg-1234", error));
|
1281 | 1293 | ASSERT_FALSE (Rust::validate_crate_name ("a+b", error));
|
1282 | 1294 | ASSERT_FALSE (Rust::validate_crate_name ("/a+b/", error));
|
| 1295 | + ASSERT_FALSE (Rust::validate_crate_name ("😸++", error)); |
| 1296 | + ASSERT_FALSE (Rust::validate_crate_name ("∀", error)); |
1283 | 1297 |
|
1284 | 1298 | /* Tests for crate name inference */
|
1285 | 1299 | ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");
|
|
0 commit comments