@@ -85,25 +85,28 @@ infer_crate_name (const std::string &filename)
8585 return crate;
8686}
8787
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)
9093{
9194 if (crate_name.empty ())
9295 {
93- rust_error_at (Location (), " crate name cannot be empty" );
96+ error = Error (Location (), " crate name cannot be empty" );
9497 return false ;
9598 }
9699 if (crate_name.length () > kMaxNameLength )
97100 {
98- rust_error_at (Location (), " crate name cannot exceed %ld characters" ,
101+ error = Error (Location (), " crate name cannot exceed %ld characters" ,
99102 kMaxNameLength );
100103 return false ;
101104 }
102105 for (auto &c : crate_name)
103106 {
104107 if (!(ISALNUM (c) || c == ' _' ))
105108 {
106- rust_error_at (Location (),
109+ error = Error (Location (),
107110 " invalid character %<%c%> in crate name: %<%s%>" , c,
108111 crate_name.c_str ());
109112 return false ;
@@ -399,7 +402,16 @@ Session::handle_option (
399402 case OPT_frust_crate_:
400403 // set the crate name
401404 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+ }
403415 else
404416 ret = false ;
405417 break ;
@@ -541,17 +553,20 @@ Session::parse_files (int num_files, const char **files)
541553 filename = files[0 ];
542554
543555 auto crate_name = infer_crate_name (filename);
556+ Error error ((Location ()), std::string ());
544557 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 ))
546559 {
547560 // fake a linemapping so that we can show the filename
548561 linemap->start_file (filename, 0 );
549562 linemap->start_line (0 , 1 );
563+ error.emit_error ();
550564 rust_inform (linemap->get_location (0 ),
551565 " crate name inferred from this file" );
552566 linemap->stop ();
553567 return ;
554568 }
569+ options.set_crate_name (crate_name);
555570 }
556571
557572 auto mappings = Analysis::Mappings::get ();
@@ -1202,16 +1217,17 @@ namespace selftest {
12021217void
12031218rust_crate_name_validation_test (void )
12041219{
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));
12081224 // FIXME: The next test does not pass as of current implementation
12091225 // ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
12101226 // 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 ));
12151231
12161232 /* Tests for crate name inference */
12171233 ASSERT_EQ (Rust::infer_crate_name (" c.rs" ), " c" );
0 commit comments