Repository: bazelbuild/bazel
Issue: Compilation failure in C++ runfiles.h under Werror=shadow
Labels: type: bug
team-Rules-CPP
untriaged

### Description of the bug:


This commit at 8f28513893969b6346d965cab12aac69cb246ced introduces a typo in the `Runfiles` constructor:

https://github.com/bazelbuild/bazel/blob/44918c540250a9e87eba8088e5676e3346695f29/tools/cpp/runfiles/runfiles_src.h#L193-L203

Note that the argument name on L198 is `std::string source_repository_` with an underscore; only member fields should end with an underscore.

When compiled using `-Werror=shadow`, GCC fails:

```
external/bazel_tools/tools/cpp/runfiles/runfiles.h: In constructor 'bazel::tools::cpp::runfiles::Runfiles::Runfiles(std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, std::string, std::map<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >, std::vector<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, std::string)':
external/bazel_tools/tools/cpp/runfiles/runfiles.h:199:7: error: declaration of 'source_repository_' shadows a member of 'bazel::tools::cpp::runfiles::Runfiles' [-Werror=shadow]
  199 |       : runfiles_map_(std::move(runfiles_map)),
      |       ^
external/bazel_tools/tools/cpp/runfiles/runfiles.h:219:21: note: shadowed declaration is here
  219 |   const std::string source_repository_;
      |                     ^~~~~~~~~~~~~~~~~~
```

This is from GCC 9.4.0 on Ubuntu 20.04, but I suspect most all versions would likewise fail.

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.


Any C++ code that does `#include "tools/cpp/runfiles/runfiles.h"` built with `bazel build --copt=-Werror=shadow` should demonstrate this.  I can provide a full repro if needed, but it doesn't seem necessary in this case.

### Which operating system are you running Bazel on?


Ubuntu 20.04

### What is the output of `bazel info release`?


6.0.0rc2

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.


_No response_

### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?


_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

This is a regression in the 6.0.0 release candidate.

Issue comments:
@bazel-io flag
@jwnimmer-tri Thanks for the report. Does #16801 look reasonable to you?
@bazel-io fork 6.0.0

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/cpp/runfiles/runfiles_src.h b/tools/cpp/runfiles/runfiles_src.h
index 75103e230bf8ce..242b834856553f 100644
--- a/tools/cpp/runfiles/runfiles_src.h
+++ b/tools/cpp/runfiles/runfiles_src.h
@@ -195,12 +195,12 @@ class Runfiles {
       std::map<std::string, std::string> runfiles_map, std::string directory,
       std::map<std::pair<std::string, std::string>, std::string> repo_mapping,
       std::vector<std::pair<std::string, std::string> > envvars,
-      std::string source_repository_)
+      std::string source_repository)
       : runfiles_map_(std::move(runfiles_map)),
         directory_(std::move(directory)),
         repo_mapping_(std::move(repo_mapping)),
         envvars_(std::move(envvars)),
-        source_repository_(std::move(source_repository_)) {}
+        source_repository_(std::move(source_repository)) {}
   Runfiles(const Runfiles&) = delete;
   Runfiles(Runfiles&&) = delete;
   Runfiles& operator=(const Runfiles&) = delete;


Test patch: