Skip to content

Fix build break in SemaHLSL.cpp on MSVC 2022: warning C4715: 'getResourceClass': not all control paths return a value #112767

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

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static ResourceClass getResourceClass(RegisterType RT) {
return ResourceClass::Sampler;
case RegisterType::C:
case RegisterType::I:
default:
llvm_unreachable("unexpected RegisterType value");
}
Copy link
Contributor

@mizvekov mizvekov Oct 17, 2024

Choose a reason for hiding this comment

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

There is a very useful warning about switching on an enum where not all enumerators are covered.

Adding a default case suppresses this warning, but this is undesirable, as we want to see this warning pop up when we add a new enumerator.

Suggested change
default:
llvm_unreachable("unexpected RegisterType value");
}
llvm_unreachable("unexpected RegisterType value");
}
llvm_unreachable("unhandled RegisterType");

Copy link
Member

@farzonl farzonl Oct 18, 2024

Choose a reason for hiding this comment

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

I don't agree with the suggested change of not using default.

@hekota says:

the switch covers all enum values.

It sounds like line 107 will be impossible to hit.

So then whats the difference between an unexpected and an unhandled register type? Why couldn't we have just done a default case?

If a new Register Type were added it would hit a llvm_unreachable on the default case and an implementer would quickly know to implement it from the call stack. I don't like the idea of changes to make a warning happy when that code change adds undistinguishable terminology between unexpected and unhandled. I would prefer not adding new terminology that would be confusing and instead add a default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It sounds like line 107 will be impossible to hit.

Not impossible: someone can always cast an arbitrary value into the enum type, which is what MSVC is trying to guard against.

As a compromise we could keep the unreachable at the bottom, and then deliberately fall through to that in the unhandled cases:

  case RegisterType::C:
  case RegisterType::I:
    // Deliberately falling through to the unreachable below.
    break;
  }
  llvm_unreachable("unexpected RegisterType value");
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

As a compromise we could keep the unreachable at the bottom, and then deliberately fall through to that in the unhandled cases:

FWIW, that's the usual approach we take.

Copy link
Member

@farzonl farzonl Oct 18, 2024

Choose a reason for hiding this comment

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

I'm fine with that. We do that in getRegisterType.

}
Expand Down
Loading