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

RangeLimits naming: inconsistent meaning of "half open" #1329

Closed
dtolnay opened this issue Feb 7, 2023 · 3 comments · Fixed by #1330
Closed

RangeLimits naming: inconsistent meaning of "half open" #1329

dtolnay opened this issue Feb 7, 2023 · 3 comments · Fixed by #1330

Comments

@dtolnay
Copy link
Member

dtolnay commented Feb 7, 2023

In dtolnay/syn#1373 I noticed an inconsistency between how ranges are described in the reference vs how the compiler uses the same terminology.

Rustc has the following enum which distinguishes .. vs ..= in range patterns and expressions:
https://github.com/rust-lang/rust/blob/1.67.0/compiler/rustc_ast/src/ast.rs#L1323-L1328

pub enum RangeLimits {
    /// Inclusive at the beginning, exclusive at the end (..)
    HalfOpen,
    /// Inclusive at the beginning and end (..=)
    Closed,
}

Ranges in rustc are effectively (Option<Expr>, RangeLimits, Option<Expr>).

Meanwhile a different approach is currently taken by the Rust Reference: https://doc.rust-lang.org/1.67.0/reference/patterns.html#range-patterns

RangePattern :
    InclusiveRangePattern | HalfOpenRangePattern

InclusiveRangePattern :
    RangePatternBound `..=` RangePatternBound

HalfOpenRangePattern :
    RangePatternBound `..` | `..=` RangePatternBound

Notice how it has a ..= pattern which it refers to as half open, even though ..= is never considered half open in rustc.

range rustc terminology reference terminology
lo.. half open half open
..=hi closed (opposite of half open) half open (!)
lo..=hi closed inclusive

Do we know whether one of these uses of the "half open" terminology is more preferred? Might we reach agreement about which of rustc or the reference should be changed?

@dtolnay
Copy link
Member Author

dtolnay commented Feb 7, 2023

Mentioning @workingjubilee since the Reference's use of "half open" comes from #900.

@ehuss
Copy link
Contributor

ehuss commented Feb 7, 2023

It seem curious to me that the compiler calls that "closed". In math terms, isn't ..=hi equivalent to $(-∞,hi]$?

One option is to give the three styles separate names that are more similar to the standard library names (RangeToInclusivePattern, etc.).

@workingjubilee
Copy link
Contributor

Oh, hello. Sorry, somehow missed this amidst my notifications. I am fine with the chosen resolution. I believe I flubbed what might have been intended to be a usage of "half-closed" there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants