-
-
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
base: main
Are you sure you want to change the base?
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.
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.
|
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.
|
Good point re. the constructors. |
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.