Skip to content

Confusing [cfg]s #689

Closed
Closed

Description

@HaoliangXu

In lib.rs file
https://github.com/rust-lang/log/blob/master/src/lib.rs#L421
I find this snippet:

#[cfg(not(target_has_atomic = "ptr"))]
impl AtomicUsize {
    const fn new(v: usize) -> AtomicUsize {
        AtomicUsize { v: Cell::new(v) }
    }

    fn load(&self, _order: Ordering) -> usize {
        self.v.get()
    }

    fn store(&self, val: usize, _order: Ordering) {
        self.v.set(val)
    }

    #[cfg(target_has_atomic = "ptr")]
    fn compare_exchange(
        &self,
        current: usize,
        new: usize,
        _success: Ordering,
        _failure: Ordering,
    ) -> Result<usize, usize> {
        let prev = self.v.get();
        if current == prev {
            self.v.set(new);
        }
        Ok(prev)
    }
}

the thing confusing me is that the first cfg takes effect when target_has_atomic = "ptr" is false, then in the impl scope, compare_exchange function will never be compiled because it needs target_has_atomic="ptr" be true.
Am I right?

Activity

  1. Thomasdezeeuw commented on Jul 17, 2025

    @Thomasdezeeuw
    Collaborator

    Seems like the method is not used? I think we can remove it. Good find 👍

  2. HaoliangXu commented on Jul 19, 2025

    @HaoliangXu
    ContributorAuthor

    Thank you. I submitted a pull request and got merged, so I'm closing it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions