Skip to content

Commit 1a7391d

Browse files
committed
rust-session-manager: address comments ...
... also more closely match rustc's behavior Signed-off-by: Zixing Liu <[email protected]>
1 parent 28769ea commit 1a7391d

File tree

6 files changed

+89
-44
lines changed

6 files changed

+89
-44
lines changed

gcc/rust/rust-lang.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ run_rust_tests ()
458458
simple_assert ();
459459
rust_cfg_parser_test ();
460460
rust_privacy_ctx_test ();
461+
rust_crate_name_validation_test ();
461462
}
462463
} // namespace selftest
463464

gcc/rust/rust-session-manager.cc

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "diagnostic.h"
3636
#include "input.h"
3737
#include "rust-target.h"
38+
#include "selftest.h"
3839

3940
extern bool
4041
saw_errors (void);
@@ -55,9 +56,10 @@ const char *kHIRTypeResolutionDumpFile = "gccrs.type-resolution.dump";
5556
const char *kTargetOptionsDumpFile = "gccrs.target-options.dump";
5657

5758
const std::string kDefaultCrateName = "rust_out";
59+
const size_t kMaxNameLength = 64;
5860

5961
static std::string
60-
infer_crate_name (const std::string filename)
62+
infer_crate_name (const std::string &filename)
6163
{
6264
if (filename == "-")
6365
return kDefaultCrateName;
@@ -74,9 +76,42 @@ infer_crate_name (const std::string filename)
7476
if (ext_position != std::string::npos)
7577
crate.erase (ext_position);
7678

79+
// Replace all the '-' symbols with '_' per Rust rules
80+
for (auto &c : crate)
81+
{
82+
if (c == '-')
83+
c = '_';
84+
}
7785
return crate;
7886
}
7987

88+
bool
89+
CompileOptions::validate_crate_name (const std::string &crate_name)
90+
{
91+
if (crate_name.empty ())
92+
{
93+
rust_error_at (Location (), "crate name cannot be empty");
94+
return false;
95+
}
96+
if (crate_name.length () > kMaxNameLength)
97+
{
98+
rust_error_at (Location (), "crate name cannot exceed %ld characters",
99+
kMaxNameLength);
100+
return false;
101+
}
102+
for (auto &c : crate_name)
103+
{
104+
if (!(ISALNUM (c) || c == '_'))
105+
{
106+
rust_error_at (Location (),
107+
"invalid character %<%c%> in crate name: %<%s%>", c,
108+
crate_name.c_str ());
109+
return false;
110+
}
111+
}
112+
return true;
113+
}
114+
80115
// Implicitly enable a target_feature (and recursively enable dependencies).
81116
void
82117
Session::implicitly_enable_feature (std::string feature_name)
@@ -496,21 +531,25 @@ Session::enable_dump (std::string arg)
496531
void
497532
Session::parse_files (int num_files, const char **files)
498533
{
499-
rust_assert (num_files > 0);
500-
501534
if (options.crate_name.empty ())
502535
{
503536
/* HACK: We use the first file to infer the crate name, which might be
504537
* incorrect: since rustc only allows one file to be supplied in the
505538
* command-line */
506-
auto crate_name = infer_crate_name (files[0]);
507-
rust_debug_loc (Location (), "inferred crate name: %s",
508-
crate_name.c_str ());
539+
auto filename = "-";
540+
if (num_files > 0)
541+
filename = files[0];
542+
543+
auto crate_name = infer_crate_name (filename);
544+
rust_debug ("inferred crate name: %s", crate_name.c_str ());
509545
if (!options.set_crate_name (crate_name))
510546
{
511-
rust_inform (Location (),
512-
"crate name inferred from the input file %<%s%>",
513-
files[0]);
547+
// fake a linemapping so that we can show the filename
548+
linemap->start_file (filename, 0);
549+
linemap->start_line (0, 1);
550+
rust_inform (linemap->get_location (0),
551+
"crate name inferred from this file");
552+
linemap->stop ();
514553
return;
515554
}
516555
}
@@ -1157,3 +1196,33 @@ TargetOptions::enable_implicit_feature_reqs (std::string feature)
11571196
* - code generation
11581197
* - link */
11591198
} // namespace Rust
1199+
1200+
#if CHECKING_P
1201+
namespace selftest {
1202+
void
1203+
rust_crate_name_validation_test (void)
1204+
{
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"));
1208+
// FIXME: The next test does not pass as of current implementation
1209+
// ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
1210+
// 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/")); */
1215+
1216+
/* Tests for crate name inference */
1217+
ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");
1218+
// NOTE: ... but - is allowed when in the filename
1219+
ASSERT_EQ (Rust::infer_crate_name ("a-b.rs"), "a_b");
1220+
ASSERT_EQ (Rust::infer_crate_name ("book.rs.txt"), "book.rs");
1221+
#if defined(HAVE_DOS_BASED_FILE_SYSTEM)
1222+
ASSERT_EQ (Rust::infer_crate_name ("a\\c\\a-b.rs"), "a_b");
1223+
#else
1224+
ASSERT_EQ (Rust::infer_crate_name ("a/c/a-b.rs"), "a_b");
1225+
#endif
1226+
}
1227+
} // namespace selftest
1228+
#endif // CHECKING_P

gcc/rust/rust-session-manager.h

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ namespace HIR {
4242
struct Crate;
4343
}
4444

45-
const size_t kMaxNameLength = 64;
46-
4745
/* Data related to target, most useful for conditional compilation and
4846
* whatever. */
4947
struct TargetOptions
@@ -217,43 +215,13 @@ struct CompileOptions
217215

218216
/* Validate the crate name using the ASCII rules
219217
TODO: Support Unicode version of the rules */
220-
bool validate_crate_name (const std::string &crate_name)
221-
{
222-
if (crate_name.empty ())
223-
{
224-
rust_error_at (Location (), "crate name cannot be empty");
225-
return false;
226-
}
227-
if (crate_name.length () > kMaxNameLength)
228-
{
229-
rust_error_at (Location (), "crate name cannot exceed %ld characters",
230-
kMaxNameLength);
231-
return false;
232-
}
233-
for (auto &c : crate_name)
234-
{
235-
if (!(ISALNUM (c) || c == '_' || c == '-'))
236-
{
237-
rust_error_at (Location (),
238-
"invalid character %<%c%> in crate name: %<%s%>", c,
239-
crate_name.c_str ());
240-
return false;
241-
}
242-
}
243-
return true;
244-
}
218+
static bool validate_crate_name (const std::string &crate_name);
245219

246220
bool set_crate_name (std::string name)
247221
{
248222
if (!validate_crate_name (name))
249223
return false;
250224

251-
/* Replace all the '-' symbols with '_' per Rust rules */
252-
for (auto &c : name)
253-
{
254-
if (c == '-')
255-
c = '_';
256-
}
257225
crate_name = std::move (name);
258226
return true;
259227
}
@@ -343,4 +311,11 @@ struct Session
343311
};
344312
} // namespace Rust
345313

314+
#if CHECKING_P
315+
namespace selftest {
316+
extern void
317+
rust_crate_name_validation_test (void);
318+
}
319+
#endif // CHECKING_P
320+
346321
#endif

gcc/testsuite/rust/compile/bad=file-name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-additional-options "-fdump-tree-gimple -frust-crate=good-name" }
1+
// { dg-additional-options "-fdump-tree-gimple -frust-crate=good_name" }
22
pub fn does_nothing() {}
33
fn main() {
44
does_nothing()

gcc/testsuite/rust/compile/specify-crate-name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-additional-options "-frust-crate=fancy-crate_name -fdump-tree-gimple" }
1+
// { dg-additional-options "-frust-crate=fancy_crate_name -fdump-tree-gimple" }
22
pub fn does_nothing() {}
33
fn main() {
44
does_nothing()

0 commit comments

Comments
 (0)