Skip to content

nll: strange suggestion to "consider removing the * #54985

Closed

Description

@pnkfelix

Spawned off of #54825 (comment)

For this code:

unsafe fn foo(x: *const Box<isize>) -> Box<isize> {
let y = *x; //~ ERROR cannot move out of dereference of raw pointer
return y;
}

which produces this diagnostic output:

LL | let y = *x; //~ ERROR cannot move out of dereference of raw pointer
| ^^
| |
| cannot move out of dereference of raw pointer
| help: consider removing the `*`: `x`

@nikomatsakis made this comment about the HELP on line 8:

Pre-existing but: it is...strange that we make this suggestion. It seems almost certainly wrong. It doesn't preserve the type or "intent" of the code in any particular way...? It's also distinct from what the AST checker says, which is to suggest &*x (though I would argue that is also wrong, for similar reasons). It feels like the best suggestion might be ptr::read but I'm sort of inclined to just not suggest anything at all.

Activity

  1. added
    A-NLLArea: Non-lexical lifetimes (NLL)
    A-diagnosticsArea: Messages for errors, warnings, and lints
    NLL-diagnosticsWorking towards the "diagnostic parity" goal
    on Oct 11, 2018
  2. estebank commented on Oct 11, 2018

    @estebank
    Contributor

    fn add_move_hints(
    &self,
    error: GroupedMoveError<'tcx>,
    err: &mut DiagnosticBuilder<'a>,
    span: Span,
    ) {
    let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
    match error {
    GroupedMoveError::MovesFromPlace {
    mut binds_to,
    move_from,
    ..
    } => {
    let try_remove_deref = match move_from {
    Place::Projection(box PlaceProjection {
    elem: ProjectionElem::Deref,
    ..
    }) => true,
    _ => false,
    };
    if try_remove_deref && snippet.starts_with('*') {
    // The snippet doesn't start with `*` in (e.g.) index
    // expressions `a[b]`, which roughly desugar to
    // `*Index::index(&a, b)` or
    // `*IndexMut::index_mut(&mut a, b)`.
    err.span_suggestion_with_applicability(
    span,
    "consider removing the `*`",
    snippet[1..].to_owned(),
    Applicability::Unspecified,
    );
    } else {
    err.span_suggestion_with_applicability(
    span,
    "consider borrowing here",
    format!("&{}", snippet),
    Applicability::Unspecified,
    );
    }
    binds_to.sort();
    binds_to.dedup();
    self.add_move_error_details(err, &binds_to);
    }
    GroupedMoveError::MovesFromValue { mut binds_to, .. } => {
    binds_to.sort();
    binds_to.dedup();
    self.add_move_error_suggestions(err, &binds_to);
    self.add_move_error_details(err, &binds_to);
    }
    // No binding. Nothing to suggest.
    GroupedMoveError::OtherIllegalMove { .. } => (),
    }
    }

  3. pnkfelix commented on Oct 16, 2018

    @pnkfelix
    ContributorAuthor

    How bad might it be to just remove this suggestion entirely (vs attempting to fine tune when we emit it)? Can we easily weigh the good it brings vs the harm it causes?

  4. nikomatsakis commented on Oct 16, 2018

    @nikomatsakis
    Contributor

    I'd prefer to just remove it entirely. I think it adds pretty little value.

  5. nikomatsakis commented on Oct 16, 2018

    @nikomatsakis
    Contributor

    I could imagine using an extended error code to offer more detailed hints

  6. nikomatsakis commented on Oct 16, 2018

    @nikomatsakis
    Contributor

    or else a reference to something online?

  7. pnkfelix commented on Oct 16, 2018

    @pnkfelix
    ContributorAuthor

    marking as NLL-deferred as this should not block RC2 (or even the Release, IMO).

  8. pnkfelix commented on Mar 20, 2019

    @pnkfelix
    ContributorAuthor

    NLL triage. Tagging as P-medium priority.

  9. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on May 24, 2019
  10. oli-obk commented on May 24, 2019

    @oli-obk
    Contributor

    Diagnostics triage: marking as E-easy since it's just removing the suggestion

  11. added a commit that references this issue on Jun 7, 2019
  12. added a commit that references this issue on Jun 7, 2019
  13. awaitlink commented on Jun 17, 2019

    @awaitlink
    Contributor

    Shouldn't this be closed because #61332 is now merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.NLL-diagnosticsWorking towards the "diagnostic parity" goalP-mediumMedium priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions