Skip to content

#cfg conditional compilation does not work in object expressions #1181

@boggle

Description

@boggle
Contributor

Likely, this also holds for non toplevel functions. It is inconvenient and probably should be fixed. Example from io.rs:

obj FILE_writer(f: os::libc::FILE, res: option::t@FILE_res) {
#cfg[target="macos"]
fn fsync() { ret macos_os::fsync(f); }
}

will not compile.

Activity

brson

brson commented on Nov 16, 2011

@brson
Contributor

This is because attributes only work on items, and objects and methods are not items. Probably the best you can do in this situation is define a function inside the fsync method and conditionalize that. I believe that will work.

obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
  fn fsync() {
    fsync_()

    #[cfg(target_os = "macos")]
    fn fsync_() {  macos_os::fsync(f) }

    ...
  }
}

It probably would not be difficult to allow attributes and conditional compilation on methods as well. Attributes on general expressions is harder.

graydon

graydon commented on Jan 31, 2012

@graydon
Contributor

obj doesn't exist anymore, closing.

added a commit that references this issue on Jul 7, 2021
55e0779
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @graydon@boggle@brson

        Issue actions

          #cfg conditional compilation does not work in object expressions · Issue #1181 · rust-lang/rust