From 9ca4faebe11908f3c634a36cd95db002152c3057 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sun, 23 May 2021 12:05:13 +0200 Subject: Initialize crateNumItr in rust-hir-map to zero. valgrind complains whenever a crate number is used because they are all derived from the initial crateNumItr in the rust-hir-map Mapping which is never initialized. This isn't technically a bug because all that is required is for crate numbers to be unique. But it could cause non-deterministic behaviour when crate numbers are used for sorting. And it makes the valgrind output really noisy. With this patch and configure --enable-valgrind-annotations running rust1 should not give any valgrind errors. --- gcc/rust/util/rust-hir-map.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index a198be6..9feff33 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -198,7 +198,7 @@ public: private: Mappings (); - CrateNum crateNumItr; + CrateNum crateNumItr = 0; CrateNum currentCrateNum; std::map hirIdIter; -- cgit v1.1 From 5467ac12d7bfd204cd4f1712ecefcbb9bcab6af8 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 21 May 2021 23:36:36 +0200 Subject: Fix raw identifier parsing. Lexer::parse_raw_identifier added the first character twice. Adds tests for a simple raw identifier, a keyword as raw identifier and xfail tests for a single underscore and forbidden keyword (crate) as raw identifier. To help error diagnostics continue after parse_raw_identifier failed in Lexer::build_token. Fixes: https://github.com/Rust-GCC/gccrs/issues/426 --- gcc/rust/lex/rust-lex.cc | 7 ++++--- gcc/testsuite/rust.test/compile/raw_identifiers.rs | 3 +++ gcc/testsuite/rust.test/compile/raw_identifiers_keywords.rs | 3 +++ .../rust.test/xfail_compile/raw_identifiers_bad_keywords.rs | 3 +++ .../rust.test/xfail_compile/raw_identifiers_underscore.rs | 3 +++ 5 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/rust.test/compile/raw_identifiers.rs create mode 100644 gcc/testsuite/rust.test/compile/raw_identifiers_keywords.rs create mode 100644 gcc/testsuite/rust.test/xfail_compile/raw_identifiers_bad_keywords.rs create mode 100644 gcc/testsuite/rust.test/xfail_compile/raw_identifiers_underscore.rs (limited to 'gcc') diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index f95a47d..16fb1ad 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -743,6 +743,9 @@ Lexer::build_token () TokenPtr raw_ident_ptr = parse_raw_identifier (loc); if (raw_ident_ptr != nullptr) return raw_ident_ptr; + else + continue; /* input got parsed, it just wasn't valid. An error + was produced. */ } else { @@ -1523,11 +1526,9 @@ Lexer::parse_raw_identifier (Location loc) current_column += 2; - str += current_char; - bool first_is_underscore = current_char == '_'; - int length = 1; + int length = 0; current_char = peek_input (); // loop through entire name while (ISALPHA (current_char) || ISDIGIT (current_char) diff --git a/gcc/testsuite/rust.test/compile/raw_identifiers.rs b/gcc/testsuite/rust.test/compile/raw_identifiers.rs new file mode 100644 index 0000000..8746f33 --- /dev/null +++ b/gcc/testsuite/rust.test/compile/raw_identifiers.rs @@ -0,0 +1,3 @@ +pub fn square(num: i32) -> i32 { /* { dg-warning "used" } */ + r#num * num +} \ No newline at end of file diff --git a/gcc/testsuite/rust.test/compile/raw_identifiers_keywords.rs b/gcc/testsuite/rust.test/compile/raw_identifiers_keywords.rs new file mode 100644 index 0000000..c9aa3cf --- /dev/null +++ b/gcc/testsuite/rust.test/compile/raw_identifiers_keywords.rs @@ -0,0 +1,3 @@ +pub fn plus(r#break: i32, r#unsafe: i32) -> i32 { /* { dg-warning "used" } */ + r#break + r#unsafe +} \ No newline at end of file diff --git a/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_bad_keywords.rs b/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_bad_keywords.rs new file mode 100644 index 0000000..854d7e6 --- /dev/null +++ b/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_bad_keywords.rs @@ -0,0 +1,3 @@ +pub fn plus(n: i32, m: i32) -> i32 { + r#crate /* { dg-error "forbidden raw identifier" } */ +} diff --git a/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_underscore.rs b/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_underscore.rs new file mode 100644 index 0000000..86e9013 --- /dev/null +++ b/gcc/testsuite/rust.test/xfail_compile/raw_identifiers_underscore.rs @@ -0,0 +1,3 @@ +pub fn s(num: i32) -> i32 { + r#_ * num /* { dg-error "not a valid raw identifier" } */ +} -- cgit v1.1 From fd95c6cec373a79b7dc0210a55398061666fe14a Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 21 May 2021 18:03:00 +0200 Subject: Mention the identifier that was unused in ScanUnused. The ScanUnused class would only mention some name was unused. Add the actual identifier name that is unused to help the user. Note that this warning might produce a similar warning as the ScanDeadcode class produces if the indentifier is a function. --- gcc/rust/resolve/rust-ast-resolve-unused.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/resolve/rust-ast-resolve-unused.h b/gcc/rust/resolve/rust-ast-resolve-unused.h index bd71110..432776c 100644 --- a/gcc/rust/resolve/rust-ast-resolve-unused.h +++ b/gcc/rust/resolve/rust-ast-resolve-unused.h @@ -38,7 +38,7 @@ public: if (!r->have_references_for_node (decl_node_id) && ident.get ().at (0) != '_') { - rust_warning_at (locus, 0, "unused name"); + rust_warning_at (locus, 0, "unused name '%s'", ident.get ().c_str ()); } return true; }); -- cgit v1.1