Skip to content

Syntax of rustc --cfg with key and value is obscure. #66450

Open

Description

Hi,

I was recently defeated by rustc --cfg.

rustc -h says:

--cfg SPEC      Configure the compilation environment

Hrm, well I know that stuff like --cfg var should work, but that I should also be able to assign a value:

$ rustc --cfg key=val -
error: invalid `--cfg` argument: `key=val` (expected `key` or `key="value"`)

$ rustc --cfg key="val" -
error: invalid `--cfg` argument: `key=val` (expected `key` or `key="value"`)

Eh? I passed exactly what the error asked. And obviously the backticks can't form the solution, as they'd open a sub-shell.

The solution is to use: rustc --cfg 'key="val"'

The quoting is very specific.

Swapping the single and double quotes won't work:

$ rustc --cfg "key='val'" -
error: character literal may only contain one codepoint
 --> <quote expansion>:1:5
  |
1 | key='val'
  |     ^^^^^
help: if you meant to write a `str` literal, use double quotes
  |
1 | key="val"
  |     ^^^^^

And oddly, quoting the value is ok, but not the key:

$ rustc --cfg '"key"="val"' -
error: invalid `--cfg` argument: `"key"="val"` (expected `key` or `key="value"`)

I find this wholly unexpected and I had to grep the source code to find out how to use the argument :(

So are the current limitations of the interface intentional?

If so, we should improve the -h doc and error message.

If not, what do we want? Should --cfg accept:

  • key=val
  • 'key="val"
  • "key='val'"
  • '"key"="val"'
  • "key=val"

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-frontendArea: Compiler frontend (errors, parsing and HIR)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions