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

rustc_hir_analysis: add a helper to check function the signature mismatches #115897

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

eduardosm
Copy link
Contributor

This function is now used to check #[panic_handler], start lang item, main, #[start] and intrinsic functions.

The diagnosis produced are now closer to the ones produced by trait/impl method signature mismatch.

This is the first time I do anything with rustc_hir_analysis/rustc_hir_typeck, so comments and suggestions about things I did wrong or that could be improved will be appreciated.

@rustbot
Copy link
Collaborator

rustbot commented Sep 16, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@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 Sep 16, 2023
|
= note: expected signature `fn(&PanicInfo<'_>) -> _`
found signature `fn(&'static PanicInfo<'_>) -> _`
note: the anonymous lifetime as defined here...
Copy link
Member

Choose a reason for hiding this comment

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

This is pretty confusing. We probably want to actually replace the call to ocx.resolve_regions_and_report_errors to a manual call to ocx.infcx.resolve_regions and report any region errors manually from there with a more descriptive message.

Copy link
Contributor Author

@eduardosm eduardosm Sep 16, 2023

Choose a reason for hiding this comment

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

What message would we want here?

In the case of a trait/impl:

use std::panic::PanicInfo;

trait Tr {
    fn panic(info: &PanicInfo<'_>);
}

impl Tr for () {
    fn panic(_info: &'static PanicInfo) {
        unimplemented!();
    }
}

It is reported similarly:

error[E0308]: method not compatible with trait
 --> lib.rs:8:5
  |
8 |     fn panic(_info: &'static PanicInfo) {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
  |
  = note: expected signature `fn(&PanicInfo<'_>)`
             found signature `fn(&'static PanicInfo<'_>)`
note: the anonymous lifetime as defined here...
 --> lib.rs:8:30
  |
8 |     fn panic(_info: &'static PanicInfo) {
  |                              ^^^^^^^^^
  = note: ...does not necessarily outlive the static lifetime

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

The difference is that in the test, the note spans the whole signature instead of just PanicInfo:

note: the anonymous lifetime as defined here...
  --> $DIR/panic-handler-bad-signature-2.rs:9:1
   |
LL | fn panic(info: &'static PanicInfo) -> !
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...does not necessarily outlive the static lifetime

I'm not sure why.

EDIT: I think it depends on whether the region is BrAnon or BrNamed with kw::UnderscoreLifetime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened #115903 regarding confusing lifetime diagnosis for trait impl functions. I think fixing that its outside the scope of this PR.

pub fn check_function_signature<'tcx>(
tcx: TyCtxt<'tcx>,
cause_code: ObligationCauseCode<'tcx>,
fn_id: LocalDefId,
Copy link
Member

Choose a reason for hiding this comment

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

This can be a DefId, and you can use CRATE_DEF_ID for the cases that it's required to be a LocalDefId.


pub fn check_function_signature<'tcx>(
tcx: TyCtxt<'tcx>,
cause_code: ObligationCauseCode<'tcx>,
Copy link
Member

Choose a reason for hiding this comment

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

Can you make the caller pass in a full ObligationCause? That's usually more conventional than just passing the code.

compiler/rustc_hir_analysis/src/check/mod.rs Show resolved Hide resolved
@compiler-errors compiler-errors self-assigned this Sep 16, 2023

let expected_sig = tcx.liberate_late_bound_regions(fn_id.into(), expected_sig);

match ocx.sup(&cause, param_env, expected_sig, actual_sig) {
Copy link
Member

@compiler-errors compiler-errors Sep 16, 2023

Choose a reason for hiding this comment

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

It concerns me that we're only checking the subtyping in one direction instead of both directions (with eq) here.

We should ideally be instantiating the binder in both directions -- this should cause us to accept more code, possibly inadvertently?

@compiler-errors compiler-errors 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 Sep 16, 2023
@eduardosm
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 16, 2023
@bors
Copy link
Contributor

bors commented Sep 17, 2023

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


let expected_sig = tcx.liberate_late_bound_regions(fn_id, expected_sig);

match ocx.eq(&cause, param_env, expected_sig, actual_sig) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not how to compare two binders. This is not very much different than using sub, which I think is still wrong. You should probably wrap both binders in a FnPtr and compare them directly. The error reporting will kinda suck, but that can be dealt with differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The output changed from

   = note: expected signature `fn(&PanicInfo<'_>) -> _`
              found signature `fn(&'static PanicInfo<'_>) -> _`

to

   = note: expected fn pointer `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
              found fn pointer `for<'a> fn(&'static PanicInfo<'a>) -> _`

But only for lifetime errors.

For non-lifetime errors, it stayed expected/found signature, I believe that thanks to passing the signatures in infer::ValuePairs here (I'm not sure that's entirely correct)

err_ctxt.note_type_err(
    &mut diag,
    &cause,
    None,
    Some(infer::ValuePairs::Sigs(ExpectedFound {
        expected: tcx.liberate_late_bound_regions(fn_id, expected_sig),
        found: tcx.liberate_late_bound_regions(fn_id, actual_sig),
    })),
    err,
    false,
    false,
);

@bors
Copy link
Contributor

bors commented Sep 19, 2023

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

…atches

This function is now used to check `#[panic_handler]`, `start` lang item, `main`, `#[start]` and intrinsic functions.

The diagnosis produced are now closer to the ones produced by trait/impl method signature mismatch.
@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 21, 2023

📌 Commit 85d61b0 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 21, 2023
@bors
Copy link
Contributor

bors commented Sep 21, 2023

⌛ Testing commit 85d61b0 with merge 99b63d0...

@bors
Copy link
Contributor

bors commented Sep 22, 2023

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 99b63d0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 22, 2023
@bors bors merged commit 99b63d0 into rust-lang:master Sep 22, 2023
11 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 22, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (99b63d0): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
2.0% [1.5%, 2.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [0.7%, 0.7%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 634.138s -> 633.396s (-0.12%)
Artifact size: 317.56 MiB -> 317.61 MiB (0.02%)

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 23, 2023
Allow higher-ranked fn sigs in `ValuePairs`

For better bookkeeping -- only affects diagnostic path. Allow reporting signature mismatches like "signature"s and not "fn pointer"s.

Improves rust-lang#115897 (comment)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 23, 2023
Allow higher-ranked fn sigs in `ValuePairs`

For better bookkeeping -- only affects diagnostic path. Allow reporting signature mismatches like "signature"s and not "fn pointer"s.

Improves rust-lang#115897 (comment)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 24, 2023
Rollup merge of rust-lang#116073 - compiler-errors:poly-sigs, r=b-naber

Allow higher-ranked fn sigs in `ValuePairs`

For better bookkeeping -- only affects diagnostic path. Allow reporting signature mismatches like "signature"s and not "fn pointer"s.

Improves rust-lang#115897 (comment)
@eduardosm eduardosm deleted the check-fn-sig branch July 18, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants