@@ -85,25 +85,28 @@ infer_crate_name (const std::string &filename)
85
85
return crate;
86
86
}
87
87
88
- bool
89
- CompileOptions::validate_crate_name (const std::string &crate_name)
88
+ /* Validate the crate name using the ASCII rules
89
+ TODO: Support Unicode version of the rules */
90
+
91
+ static bool
92
+ validate_crate_name (const std::string &crate_name, Error &error)
90
93
{
91
94
if (crate_name.empty ())
92
95
{
93
- rust_error_at (Location (), " crate name cannot be empty" );
96
+ error = Error (Location (), " crate name cannot be empty" );
94
97
return false ;
95
98
}
96
99
if (crate_name.length () > kMaxNameLength )
97
100
{
98
- rust_error_at (Location (), " crate name cannot exceed %ld characters" ,
101
+ error = Error (Location (), " crate name cannot exceed %ld characters" ,
99
102
kMaxNameLength );
100
103
return false ;
101
104
}
102
105
for (auto &c : crate_name)
103
106
{
104
107
if (!(ISALNUM (c) || c == ' _' ))
105
108
{
106
- rust_error_at (Location (),
109
+ error = Error (Location (),
107
110
" invalid character %<%c%> in crate name: %<%s%>" , c,
108
111
crate_name.c_str ());
109
112
return false ;
@@ -399,7 +402,16 @@ Session::handle_option (
399
402
case OPT_frust_crate_:
400
403
// set the crate name
401
404
if (arg != nullptr )
402
- ret = options.set_crate_name (arg);
405
+ {
406
+ auto error = Error (Location (), std::string ());
407
+ if ((ret = validate_crate_name (arg, error)))
408
+ options.set_crate_name (arg);
409
+ else
410
+ {
411
+ rust_assert (!error.message .empty ());
412
+ error.emit_error ();
413
+ }
414
+ }
403
415
else
404
416
ret = false ;
405
417
break ;
@@ -541,17 +553,20 @@ Session::parse_files (int num_files, const char **files)
541
553
filename = files[0 ];
542
554
543
555
auto crate_name = infer_crate_name (filename);
556
+ Error error ((Location ()), std::string ());
544
557
rust_debug (" inferred crate name: %s" , crate_name.c_str ());
545
- if (!options. set_crate_name (crate_name))
558
+ if (!validate_crate_name (crate_name, error ))
546
559
{
547
560
// fake a linemapping so that we can show the filename
548
561
linemap->start_file (filename, 0 );
549
562
linemap->start_line (0 , 1 );
563
+ error.emit_error ();
550
564
rust_inform (linemap->get_location (0 ),
551
565
" crate name inferred from this file" );
552
566
linemap->stop ();
553
567
return ;
554
568
}
569
+ options.set_crate_name (crate_name);
555
570
}
556
571
557
572
auto mappings = Analysis::Mappings::get ();
@@ -1202,16 +1217,17 @@ namespace selftest {
1202
1217
void
1203
1218
rust_crate_name_validation_test (void )
1204
1219
{
1205
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name (" example" ));
1206
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name (" abcdefg_1234" ));
1207
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name (" 1" ));
1220
+ auto error = Rust::Error (Location (), std::string ());
1221
+ ASSERT_TRUE (Rust::validate_crate_name (" example" , error));
1222
+ ASSERT_TRUE (Rust::validate_crate_name (" abcdefg_1234" , error));
1223
+ ASSERT_TRUE (Rust::validate_crate_name (" 1" , error));
1208
1224
// FIXME: The next test does not pass as of current implementation
1209
1225
// ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
1210
1226
// NOTE: - is not allowed in the crate name ...
1211
- /*
1212
- ASSERT_FALSE (Rust::CompileOptions:: validate_crate_name ("abcdefg-1234"));
1213
- ASSERT_FALSE (Rust::CompileOptions:: validate_crate_name ("a+b"));
1214
- ASSERT_FALSE (Rust::CompileOptions:: validate_crate_name ("/a+b/")); */
1227
+
1228
+ ASSERT_FALSE (Rust::validate_crate_name (" abcdefg-1234" , error ));
1229
+ ASSERT_FALSE (Rust::validate_crate_name (" a+b" , error ));
1230
+ ASSERT_FALSE (Rust::validate_crate_name (" /a+b/" , error ));
1215
1231
1216
1232
/* Tests for crate name inference */
1217
1233
ASSERT_EQ (Rust::infer_crate_name (" c.rs" ), " c" );
0 commit comments