Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: make PartialOrd & Ord impls more correct #995

Merged
merged 2 commits into from
Sep 28, 2020

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Sep 28, 2020

Motivation

Currently, there are some minor issues with Ord and PartialOrd impls
in tracing_subscriber::filter::env:

  • The Directive and StaticDirective types implement PartialOrd
    with an implementation that never returns None, and then have Ord
    implementations that call partial_cmp and expect that the returned
    value is Some. This isn't necessary.
  • field::Match implements PartialOrd manually but derives Ord.
    Since these traits must agree, using the derived implementation for
    one but manually implementing the other is potentially incorrect (see
    fix nightly clippy warnings #991).

Solution

This branch fixes these issues. I've moved actual comparison code from
PartialOrd impls for Directive and StaticDirective to their Ord
impls, and changed PartialOrd::partial_cmp for those types to call
Ord::cmp and wrap it in a Some, rather than having Ord::cmp call
PartialOrd::partial_cmp and unwrap it. This way, the fact that these
comparison impls can never return None is encoded at the type level,
rather than having an expect that will always succeed.

Additionally, I've added a manual impl of Ord for field::Match and
removed the derived impl. This should make Clippy happier.

Signed-off-by: Eliza Weisman eliza@buoyant.io

Currently, there are some minor issues with `Ord` and `PartialOrd` impls
in `tracing_subscriber::filter::env`:

- The `Directive` and `StaticDirective` types implement `PartialOrd`
  with an implementation that never returns `None`, and then have `Ord`
  implementations that call `partial_cmp` and `expect` that the returned
  value is `Some`. This isn't necessary.
- `field::Match` implements `PartialOrd` manually but derives `Ord`.
  Since these traits must agree, using the derived implementation for
  one but manually implementing the other is potentially incorrect (see
  #991).

This branch fixes these issues. I've moved actual comparison code from
`PartialOrd` impls for `Directive` and `StaticDirective` to their `Ord`
impls, and changed `PartialOrd::partial_cmp` for those types to call
`Ord::cmp` and wrap it in a `Some`, rather than having `Ord::cmp` call
`PartialOrd::partial_cmp` and unwrap it. This way, the fact that these
comparison impls can never return `None` is encoded at the type level,
rather than having an `expect` that will always succeed.

Additionally, I've added a manual impl of `Ord` for `field::Match` and
removed the derived impl. This should make Clippy happier.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw added the crate/subscriber Related to the `tracing-subscriber` crate label Sep 28, 2020
@hawkw hawkw requested a review from a team as a code owner September 28, 2020 16:23
@hawkw hawkw self-assigned this Sep 28, 2020
@LucioFranco
Copy link
Member

Looks like some build errors?

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw merged commit fa4fece into master Sep 28, 2020
dvdplm added a commit to dvdplm/tracing that referenced this pull request Oct 7, 2020
* upstream/master:
  subscriber: warn if trying to enable a statically disabled level (tokio-rs#990)
  subscriber: use macros for module declarations (tokio-rs#1009)
  chore: remove `stdlib.rs` (tokio-rs#1008)
  core: fix linked list tests reusing `Registration`s (tokio-rs#1016)
  subscriber: support dash in target names (tokio-rs#1012)
  docs: switch to intra-doc links in tracing-core (tokio-rs#1010)
  tracing-opentelemetry: implement additional record types (bool, i64, u64) (tokio-rs#1007)
  core: add intrusive linked list for callsite registry (tokio-rs#988)
  serde: allow tracing-serde to work on no_std. (tokio-rs#960)
  tracing: remove `Into<Option<Id>>` impl for `Span` (tokio-rs#1003)
  tracing: make `Entered` `!Send` (tokio-rs#1001)
  chore: fix nightly clippy warnings (tokio-rs#991)
  chore: bump all crate versions (tokio-rs#998)
  macros: fix the `tracing-macros` crate not compiling (tokio-rs#1000)
  tracing: prepare to release 0.1.21 (tokio-rs#997)
  core: prepare to release 0.1.17 (tokio-rs#996)
  subscriber: make `PartialOrd` & `Ord` impls more correct (tokio-rs#995)
  core, tracing: don't inline dispatcher::get_default (tokio-rs#994)
  core: make `Level` and `LevelFilter` `Copy` (tokio-rs#992)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/subscriber Related to the `tracing-subscriber` crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants