Skip to content

Commit a125901

Browse files
liushuyuCohenArthur
andcommitted
rust-session-manager: address more comments
Co-authored-by: Arthur Cohen <[email protected]> Signed-off-by: Zixing Liu <[email protected]>
1 parent 1a7391d commit a125901

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

gcc/rust/rust-session-manager.cc

+30-14
Original file line numberDiff line numberDiff line change
@@ -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 {
12021217
void
12031218
rust_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");

gcc/rust/rust-session-manager.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,11 @@ struct CompileOptions
213213
enable_dump_option (DumpOption::TYPE_RESOLUTION_DUMP);
214214
}
215215

216-
/* Validate the crate name using the ASCII rules
217-
TODO: Support Unicode version of the rules */
218-
static bool validate_crate_name (const std::string &crate_name);
219-
220-
bool set_crate_name (std::string name)
216+
void set_crate_name (std::string name)
221217
{
222-
if (!validate_crate_name (name))
223-
return false;
218+
rust_assert (!name.empty ());
224219

225220
crate_name = std::move (name);
226-
return true;
227221
}
228222

229223
void set_edition (int raw_edition)

0 commit comments

Comments
 (0)