Skip to content

cfg() and multiple predicates, logic language, etc #2119

Closed
@olsonjeffery

Description

@olsonjeffery

Quick over: the cfg() attribute can't be used to do multiple-declaration of rust types/fns/mods along multiple-dimensions of consideration (OS, arch) without signifigant hurdle-jumping-through'ing. I spoke with @graydon about this, briefly, on IRC and he was under the impression that commas in a list passed to cfg() would behave like an &&, but it seems, from my observation, that they act more like ||.

Here's a test case:

use std;
import io;

fn main() {
  io::println("#[cfg()] failure test case");
  let inst = new_foo_inst();
}

#[cfg(target_os="linux", target_arch="x86_64")]
type foo = {
  a: int, b: int
};
#[cfg(target_os="linux", target_arch="x86")]
type foo = {
  a: int, b: int, c: int
};

fn new_foo_inst() -> foo {
  ret new_foo_arch();
  #[cfg(target_arch="x86_64")]
  fn new_foo_arch() -> foo {
    ret { a: 0, b: 0 };
  }
  #[cfg(target_arch="x86")]
  fn new_foo_arch() -> foo {
    ret { a: 0, b: 0, c: 0};
  }
}

Will produce this compiler output:

./cfg.rs:10:0: 12:2 error: duplicate definition of type foo
./cfg.rs:10 type foo = {
./cfg.rs:11   a: int, b: int
./cfg.rs:12 };

There are workarounds, of course (As my new_foo_inst() impl shows, can acheive similar with nested branches of os::arch modules). But I think we can do better.

I know this isn't high-priority (as I mentioned, I've already worked around the problem... and I'm not after a turing-completeness for cfg() :) , but it would propose changes the cfg() impl to:

  1. Change the , in cfg() to be ||.
  2. Add && as a supported separator, with all that it implies
  3. Support arbitrary, if-like predicate nesting within cfg() using parenthesis

Also, interesting that a single = is used for "equals", here. Trying hard to avoid the impulse to not be pedantic about consistency across the language (== is used for such things in the main grammar).

This is totally outside the level of my experience with the language internals so far, and I gather would involve syntax changes. I'm interested in getting my feet wet, gently, with this. Assuming that such a change would be accepted, where would I (or anyone else interested in this work) start?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions