-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
compiletest: Add LineNumber newtype to avoid +1 magic here and there
#150205
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
Conversation
|
Some changes occurred in src/tools/compiletest cc @jieyouxu |
This comment has been minimized.
This comment has been minimized.
5a015a9 to
334900c
Compare
LineNo newtype to avoid +1 magic here and thereLineNumber newtype to avoid +1 magic here and there
This comment has been minimized.
This comment has been minimized.
|
I've done some additional verification and code cleanup and am happy with how this works and looks. Ready for review! |
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good, thanks
|
@bors r+ rollup |
| /// Create a LineNumber from a zero-based line index. I.e. if `zero_based` | ||
| /// is `0` it means "the first line". | ||
| pub(crate) fn from_zero_based(zero_based: usize) -> Self { | ||
| // Ensure to panic on overflow. | ||
| LineNumber(zero_based.strict_add(1)) | ||
| } | ||
|
|
||
| pub(crate) fn from_one_based(one_based: usize) -> LineNumber { | ||
| // You can't pass zero here. Use .none() for "no specific line". | ||
| assert!(one_based > 0); | ||
| LineNumber(one_based) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These constructors seem overly complicated and unnecessary to me.
From what I can tell, every call site would be made much simpler by just calling a single LineNumber::enumerate(..) method that always starts from 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that's a lot nicer, thanks. Done.
|
Good point re. the constructors. |
|
r? @Zalathar (off review for a few days) |
This comment has been minimized.
This comment has been minimized.
Start small. If it works well we can increase usage bit by bit as time passes. Note that we keep using "0" to represent "no specific line" because changing to `Option<LineNumber>` everywhere is much bigger and noisier change. That can be done later if wanted.
|
Thanks for the adjustments. @bors r=Zalathar,jieyouxu |
compiletest: Add `LineNumber` newtype to avoid `+1` magic here and there Start small. If it works well we can increase usage bit by bit as time passes. My main motivation for doing this is to get rid of the `+ 1` I otherwise have to add in rust-lang#150201 on this line: ```rs crate::directives::line::line_directive(file, zero_based_line_no + 1, &line) ``` But I think this is a nice general improvement by itself. Note that we keep using "0" to represent "no specific line" because changing to `Option<LineNumber>` everywhere is a very noisy and significant change. That _can_ be changed later if wanted, but let's not do it now.
…uwer Rollup of 6 pull requests Successful merges: - #150130 (Support syntax for one-line trait reuse) - #150205 (compiletest: Add `LineNumber` newtype to avoid `+1` magic here and there) - #150282 (move a ui test to coretests unit test) - #150295 (Fix compilation error in hermit-abi time.rs) - #150301 (std: remove unsupported pipe module from VEXos pal) - #150303 (`target_features::Stability`: tweak docs of `requires_nightly()`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #150205 - Enselic:line-no, r=Zalathar,jieyouxu compiletest: Add `LineNumber` newtype to avoid `+1` magic here and there Start small. If it works well we can increase usage bit by bit as time passes. My main motivation for doing this is to get rid of the `+ 1` I otherwise have to add in #150201 on this line: ```rs crate::directives::line::line_directive(file, zero_based_line_no + 1, &line) ``` But I think this is a nice general improvement by itself. Note that we keep using "0" to represent "no specific line" because changing to `Option<LineNumber>` everywhere is a very noisy and significant change. That _can_ be changed later if wanted, but let's not do it now.
|
|
||
| impl LineNumber { | ||
| /// This represents "no specific line" (used e.g. for implied directives). | ||
| pub(crate) const ZERO: Self = Self(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could call this ANY or similar to better communicate intent. ZERO seems more like an implementation detail
Start small. If it works well we can increase usage bit by bit as time passes.
My main motivation for doing this is to get rid of the
+ 1I otherwise have to add in #150201 on this line:But I think this is a nice general improvement by itself.
Note that we keep using "0" to represent "no specific line" because changing to
Option<LineNumber>everywhere is a very noisy and significant change. That can be changed later if wanted, but let's not do it now.