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

Use rustc attrs in dangling_pointers_from_temporaries lint #132281

Open
Urgau opened this issue Oct 28, 2024 · 6 comments
Open

Use rustc attrs in dangling_pointers_from_temporaries lint #132281

Urgau opened this issue Oct 28, 2024 · 6 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Urgau
Copy link
Member

Urgau commented Oct 28, 2024

If you want to do it in this PR (otherwise in a follow-up), we could add an rustc attribute to those as_ptr/as_mut_ptr methods.

You can follow afcb09b and 2a930d3 (from a PR of mine) with check_applied_to_fn_or_method instead, and something similar to this to check the attribute

&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)

Originally posted by @Urgau in #128985 (comment)

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2024
@Urgau Urgau added C-cleanup Category: PRs that clean code up or issues documenting cleanup. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 28, 2024
@Urgau Urgau added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Oct 29, 2024
@Urgau
Copy link
Member Author

Urgau commented Oct 29, 2024

Steps

  1. Create the attribute (symbol, register, and validation), let's say rustc_as_ptr_function (better name to be found)
  2. Apply the newly created attribute to the relevant as_ptr/as_mut_ptr methods to the types listed
    // Array, Vec, String, CString, MaybeUninit, Cell, Box<[_]>, Box<str>, Box<CStr>,
    // or any of the above in arbitrary many nested Box'es.
    fn is_interesting(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool {
    if ty.is_array() {
    true
    } else if let Some(inner) = ty.boxed_ty() {
    inner.is_slice()
    || inner.is_str()
    || inner.ty_adt_def().is_some_and(|def| tcx.is_lang_item(def.did(), LangItem::CStr))
    || is_interesting(tcx, inner)
    } else if let Some(def) = ty.ty_adt_def() {
    for lang_item in [LangItem::String, LangItem::MaybeUninit] {
    if tcx.is_lang_item(def.did(), lang_item) {
    return true;
    }
    }
    tcx.get_diagnostic_name(def.did())
    .is_some_and(|name| matches!(name, sym::cstring_type | sym::Vec | sym::Cell))
  3. Use the newly created attribute instead of is_interesting fn, with something similar to this
    && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
    && cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)

More context in #128985 (comment)

@gavincrawford

This comment has been minimized.

@rustbot

This comment has been minimized.

@gavincrawford
Copy link

@rustbot claim
Oops

@gavincrawford
Copy link

@GrigorenkoPV sorry, didn't realize this your follow up issue. Apologies.
@rustbot release-assignment

@GrigorenkoPV
Copy link
Contributor

@GrigorenkoPV sorry, didn't realize this your follow up issue. Apologies. @rustbot release-assignment

I, most likely, won't have a chance to work on it in the next few weeks, so you can claim it in the meantime if you want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants