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

lint / ImproperCTypes: better handling of indirections, take 2 #134697

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

niacdoial
Copy link
Contributor

This PR tries to re-add the changes in #131669 (which were reverted in #134064 after one (1) nightly),
and adds better coverage of ty_kinds:

  • in the take-1-added TypeSizedness enum and its construction
  • in a new test file

The changes in the original PR aim to make ImproperCTypes/ImproperCTypesDefinitions produce better warnings when dealing with indirections (Box, &T, *T), especially for those to DSTs.

r? workingjubilee (because you reviewed the first attempt)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2024
@niacdoial
Copy link
Contributor Author

(
hi Jubilee, I'm back at this again!
I know this is not the best time of year to add PRs, so I'm fine with postponing this if you don't feel like tackling it these upcoming weeks.
In any case, have some nice end-of-year festivities, if you celebrate any!
)

@niacdoial
Copy link
Contributor Author

ah, and before I forget: a small part of the new test file is commented out because it hits ICE #134587, but there should be more than decent coverage anyway

@workingjubilee
Copy link
Member

unfortunately the lint needs to be gutted and rewritten.

@workingjubilee
Copy link
Member

workingjubilee commented Dec 24, 2024

Also while I was possibly having a mild case of get-there-itis and thus mostly tried to just make sure things were coherent, I would prefer all new code for the lint be in compiler/rustc_lint/src/types/improper_ctypes.rs.

@workingjubilee workingjubilee added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 4, 2025
@workingjubilee
Copy link
Member

The version cut happened so there will be less time pressure now.

@niacdoial
Copy link
Contributor Author

I have a first version for compiler/rustc_lint/src/types/improper_ctypes.rs if you want.
it's less of a from-the-ground-up rewrite as it is scrapping the original for parts, if the analogy makes sense.

you probably have things to say about its architecture, even if the whole thing still have a bunch of TODO comments
the progress so far looks like this:

  • completely separate the type-checking and reporting systems
  • (part of the way there) remove some of the special cases and integrate them to the "main logic"
    • check_for_opaque_types is still a "special case" part of the checking logic
    • Cstr and Cstring are also somewhat special-cased because the advice for them depends on the type around them, if any
    • the unit type is handled in multiple places, see if this can be fixed
  • (almost complete) compile, pass existing tests
    • only failed tests are for Cstring, due to different error messages
    • one unrelated test had to have a second "#[allow(improper_ctypes)]" added, but it makes more sense for it to need that anyway
  • better separation of the different checks in different visit_* methods of ImproperCTypesVisitor
  • better tracking of how the currently-checked type is used (static, function argument, function return's inner type, etc...)
    • raises questions about the separation of improper_ctypes and improper_ctypes_definitions versus declared/defined functions, especially when FnPtr:s are involved
  • allow single argument check to emit multiple errors (for fnptr:s, structures with multiple FFI-unsafe fields, etc)
  • review what is considered FFI-safe or not (once everything else is complete)

If you want to take a look in this state, should I just commit it here? (possibly put the PR in draft mode while I'm at it?)
or send you the files in a different way?

@bors
Copy link
Contributor

bors commented Jan 15, 2025

☔ The latest upstream changes (presumably #135525) made this pull request unmergeable. Please resolve the merge conflicts.

- removed special-case logic for a few cases (including the unit type,
  which is now only checked for in one place)
- moved a lot of type-checking code in their dedicated visit_* methods
- reworked FfiResult type to handle multiple diagnostics per type
  (currently imperfect due to type caching)
- reworked the messages around CStr and CString
@niacdoial
Copy link
Contributor Author

niacdoial commented Jan 18, 2025

aaaand I think I have something that's "first draft" material! (should I put this pull in the "draft" state?)
There's still a bunch of TODOs (...well, they were changed to FIXMEs to be pushed here) for questions I didn't manage to answer (and a bunch of failing tests because I don't know if the error should be here), but yeah.

here's a list of some of my remaining questions and concerns:

  • visit_numeric seems too x86_64-specific

  • should we revisit the distinction between ImproperCTypes and ImproperCTypesDefinitions?

    • part 1: the output ("external fn" or vs "external block" vs other possibilities)
    • part 2: handling opaque types (there's a high correlation between ImproperCTypesDefinitions and places where we allow FFI-opaque types to be fully specified. do we want this correlation to be 1?)
  • more on FFI-opaque types: how do we handle that in the context of the "context switch" between functions and possible FnPtr arguments? The answer that seems correct currently prevents a stage1 compiler from being built

    • should we introduce a std::ffi::FfiOpaquePtr type? (which would be a *const c_void and some phantomdata, on first approximation)
  • for indirections whose values may be supplied by non-rust code: do we only allow pointers (and Optionstd::ptr::NonNull), or do we also allow Option<&T> and Option<Box<T>>?

  • not sure if the new error messages are intelligible in all cases (especially if there's a type param like Self or <Self as ::std::ops::Add<Self>>::Output that gets resolved in the error message).

  • it feels like the current handling of CStr/Cstring and Option-like enums uses special casing, since those are tested for in multiple places.

  • if we deny references and boxes in defined functions, what of &self in methods? We don't allow *const Self, last I checked.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling time-core v0.1.2
   Compiling time-macros v0.2.19
   Compiling deranged v0.3.11
   Compiling rustc-main v0.0.0 (/checkout/compiler/rustc)
error: `extern` fn uses type `&RustString`, which is not FFI-safe
   |
68 |     buf: &RustString,
   |          ^^^^^^^^^^^ not FFI-safe
   |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants