From f28ac8e17ecf6b59d4731db4860bad0ccf9c5b08 Mon Sep 17 00:00:00 2001 From: Dylan Gardner Date: Thu, 29 Aug 2024 04:43:42 -0700 Subject: [PATCH] Infer crate name after file opening Fixes #3129. gcc/rust/ChangeLog: * rust-session-manager.cc (Session::handle_crate_name): Remove crate name inference (Session::compile_crate): Add crate name inference and error if inferred name is empty. Remove CompileOptions::get_instance () that returned a local copy of the options. Rename crate_name_changed to crate_name_found to match semantics. (rust_crate_name_validation_test): Test inferring ".rs" name * rust-session-manager.h: Modify handle_crate_name definition to include filename. --- gcc/rust/rust-session-manager.cc | 61 +++++++++++++++++--------------- gcc/rust/rust-session-manager.h | 2 +- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index f132cf99f140..704cb3fe1cde 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -396,31 +396,16 @@ Session::handle_input_files (int num_files, const char **files) const auto &file = files[0]; - if (options.crate_name.empty ()) - { - auto filename = "-"; - if (num_files > 0) - filename = files[0]; - - auto crate_name = infer_crate_name (filename); - rust_debug ("inferred crate name: %s", crate_name.c_str ()); - // set the preliminary crate name here - // we will figure out the real crate name in `handle_crate_name` - options.set_crate_name (crate_name); - } - - CrateNum crate_num = mappings.get_next_crate_num (options.get_crate_name ()); - mappings.set_current_crate (crate_num); - rust_debug ("Attempting to parse file: %s", file); compile_crate (file); } void -Session::handle_crate_name (const AST::Crate &parsed_crate) +Session::handle_crate_name (const char *filename, + const AST::Crate &parsed_crate) { auto &mappings = Analysis::Mappings::get (); - auto crate_name_changed = false; + auto crate_name_found = false; auto error = Error (UNDEF_LOCATION, std::string ()); for (const auto &attr : parsed_crate.inner_attrs) @@ -444,7 +429,6 @@ Session::handle_crate_name (const AST::Crate &parsed_crate) continue; } - auto options = Session::get_instance ().options; if (options.crate_name_set_manually && (options.crate_name != msg_str)) { rust_error_at (attr.get_locus (), @@ -452,19 +436,39 @@ Session::handle_crate_name (const AST::Crate &parsed_crate) "required to match, but %qs does not match %qs", options.crate_name.c_str (), msg_str.c_str ()); } - crate_name_changed = true; + crate_name_found = true; options.set_crate_name (msg_str); - mappings.set_crate_name (mappings.get_current_crate (), msg_str); } - options.crate_name_set_manually |= crate_name_changed; - if (!options.crate_name_set_manually - && !validate_crate_name (options.crate_name, error)) + options.crate_name_set_manually |= crate_name_found; + if (!options.crate_name_set_manually) { - error.emit (); - rust_inform (linemap_position_for_column (line_table, 0), - "crate name inferred from this file"); + auto crate_name = infer_crate_name (filename); + if (crate_name.empty ()) + { + rust_error_at (UNDEF_LOCATION, "crate name is empty", filename); + rust_inform (linemap_position_for_column (line_table, 0), + "crate name inferred from this file"); + return; + } + + rust_debug ("inferred crate name: %s", crate_name.c_str ()); + options.set_crate_name (crate_name); + + if (!validate_crate_name (options.get_crate_name (), error)) + { + error.emit (); + rust_inform (linemap_position_for_column (line_table, 0), + "crate name inferred from this file"); + return; + } } + + if (saw_errors ()) + return; + + CrateNum crate_num = mappings.get_next_crate_num (options.get_crate_name ()); + mappings.set_current_crate (crate_num); } // Parses a single file with filename filename. @@ -533,7 +537,7 @@ Session::compile_crate (const char *filename) std::unique_ptr ast_crate = parser.parse_crate (); // handle crate name - handle_crate_name (*ast_crate.get ()); + handle_crate_name (filename, *ast_crate.get ()); // dump options except lexer dump if (options.dump_option_enabled (CompileOptions::AST_DUMP_PRETTY)) @@ -1384,6 +1388,7 @@ rust_crate_name_validation_test (void) ASSERT_FALSE (Rust::validate_crate_name ("∀", error)); /* Tests for crate name inference */ + ASSERT_EQ (Rust::infer_crate_name (".rs"), ""); ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c"); // NOTE: ... but - is allowed when in the filename ASSERT_EQ (Rust::infer_crate_name ("a-b.rs"), "a_b"); diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h index 17b52bae4b1d..6af4ee0fcfed 100644 --- a/gcc/rust/rust-session-manager.h +++ b/gcc/rust/rust-session-manager.h @@ -376,7 +376,7 @@ struct Session const struct cl_option_handlers *handlers); void handle_input_files (int num_files, const char **files); void init_options (); - void handle_crate_name (const AST::Crate &parsed_crate); + void handle_crate_name (const char *filename, const AST::Crate &parsed_crate); /* This function saves the filename data into the session manager using the * `move` semantics, and returns a C-style string referencing the input