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

Codechange: make explicit when a TileIndex is cast to its basetype #11190

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

TrueBrain
Copy link
Member

@TrueBrain TrueBrain commented Aug 12, 2023

This PR includes #10761 as it depends on it.

Motivation / Problem

StrongTypeDef allowed, before #10761 already, implicit conversion to uint32_t for TileIndex, because of the many many many cases this was needed. But because of #10761, the amount of places this is still happening is reduced by a lot. This is mostly because functions like SetDParam now accept a StrongTypeDef, and knows how to deal with it.

This made me wonder: can we make TileIndex's conversion explicit, so we don't accidentally assign incompatible types to each other just because the compiler used an implicit conversion.

Description

Turns out, the answer is: yes, we can. And this PR is the result of that.

dest_tile is misused by disasters to store an ID. Not part of this PR to ... "fix" that issue.

The TileIndexDiff is a bit weird; that is not normally how we calculate a TileIndexDiff. So changed it to what others do too.

Limitations

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

src/build_vehicle_gui.cpp Outdated Show resolved Hide resolved
@TrueBrain TrueBrain force-pushed the strong-type-tileindex branch 2 times, most recently from 23e20ba to c5e6521 Compare August 12, 2023 18:42
src/newgrf_object.cpp Outdated Show resolved Hide resolved
src/company_cmd.cpp Show resolved Hide resolved
@@ -151,7 +151,7 @@ Industry::~Industry()
for (TileIndex tile_cur : this->location) {
if (IsTileType(tile_cur, MP_INDUSTRY)) {
if (GetIndustryIndex(tile_cur) == this->index) {
DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur);
DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, static_cast<uint32_t>(tile_cur));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function shows up a lot in this PR. Could it be templated to accept a TileIndex without casting each time it's called?

Copy link
Member Author

@TrueBrain TrueBrain Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a balance. I didn't want to silently case the strong-typed values for the newgrf inspect window, as honestly, that felt a bit wrong in these cases. It depends on the enum what the type of the second thing is, and that deserves its own proper solution. But not for this PR.

Similar can be argued for the window-related functions, but there were just too many of them to do it like that.

So yeah, meh. I don't want to sparkles templated functions everywhere; for SetDParam and window_number I can see the argument. Here .. I was much more hesitant. So that is why I went for this approach. I rather not add templates functions to just cast away the strong typing when possible. "Work by exception" approach :)

Let me know if you disagree. It is an opinion, not a black/white thing :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I'm qualified to have an opinion, but looking through code with casts everywhere can be confusing. And we know the timeline of "we'll give it a proper solution in another PR someday." 😛

I wonder if a function overload would clean up the function calls to cast in one central place, or if that's just half-assing the eventual correct solution.

In any case, none of it bothers me enough to prevent an approval. If you decide to change it, I'll re-approve.

This prevents people accidentially assigning a TileIndex to a Date
or any other type they shouldn't.
@TrueBrain TrueBrain merged commit 0773058 into OpenTTD:master Aug 15, 2023
20 checks passed
@TrueBrain TrueBrain deleted the strong-type-tileindex branch August 15, 2023 16:12
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 this pull request may close these issues.

None yet

3 participants