Skip to content

failed to automatically apply fixes suggested by rustc due to use of moved value #103776

Description

@Jerrody

I tried this code:

let conn = format!("{conn}/sql");
	// Create a new terminal REPL
	let mut rl = Editor::<()>::new().unwrap();
	// Load the command-line history
	let _ = rl.load_history("history.txt");
	// Loop over each command-line input
	loop {
		// Prompt the user to input SQL
		let readline = rl.readline("> ");
		// Check the user input
		match readline {
			// The user typed a query
			Ok(line) => {
				// Ignore all empty lines
				if line.is_empty() {
					continue;
				}
				// Add the entry to the history
				rl.add_history_entry(line.as_str());
				// Make a new remote request
				let res = Client::new()
					.post(&conn)
					.header(ACCEPT, "application/json")
					.basic_auth(user, Some(pass));
				// Add NS header if specified
				let res = match ns {
					Some(ns) => res.header("NS", ns),
					None => res,
				};
				// Add DB header if specified
				let res = match db {
					Some(db) => res.header("DB", db),
					None => res,
				};
				// Complete request
				let res = res.body(line).send();
				// Get the request response
				match process(pretty, res) {
					Ok(v) => println!("{}", v),
					Err(e) => eprintln!("{}", e),
				}
			}
			// The user types CTRL-C
			Err(ReadlineError::Interrupted) => {
				break;
			}
			// The user typed CTRL-D
			Err(ReadlineError::Eof) => {
				break;
			}
			// There was en error
			Err(err) => {
				eprintln!("Error: {:?}", err);
				break;
			}
		}
	}

This block of code (43):

let res = Client::new()
    .post(&conn) // HERE.
    .header(ACCEPT, "application/json")

I expected to see this happen:
I used cargo clippy --fix --allow-dirty and everything fixes correct.

Instead, this happened:

warning: `surrealdb` (lib) generated 6 warnings
warning: failed to automatically apply fixes suggested by rustc to crate `surreal`

after fixes were automatically applied the compiler reported errors within these files:

  * src\cli\sql.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0382]: use of moved value: `conn`
  --> src\cli\sql.rs:43:12
   |
22 |     let conn = format!("{conn}/sql");
   |         ---- move occurs because `conn` has type `std::string::String`, which does not implement the `Copy` trait
...
43 |                     .post(conn)
   |                           ^^^^ value moved here, in previous iteration of loop

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (1898c34e9 2022-10-26)
binary: rustc
commit-hash: 1898c34e923bad763e723c68dd9f23a09f9eb0fc
commit-date: 2022-10-26
host: x86_64-pc-windows-msvc
release: 1.66.0-nightly
LLVM version: 15.0.2
Backtrace

// NO BACKTRACE IT JUST FALES TO FIX.

Activity

  1. changed the title [-]BUG: `failed to automatically apply fixes suggested by rustc` due to `use of moved value: `conn``[/-] [+]BUG: `failed to automatically apply fixes suggested by rustc` due to `use of moved value: `conn`.[/+] on Oct 30, 2022
  2. changed the title [-]BUG: `failed to automatically apply fixes suggested by rustc` due to `use of moved value: `conn`.[/-] [+]BUG: `failed to automatically apply fixes suggested by rustc` due to `use of moved value: conn`.[/+] on Oct 30, 2022
  3. compiler-errors commented on Oct 30, 2022

    @compiler-errors
    Contributor

    Do you mind sharing what the suggested fix actually was? It's hard to tell what was applied just from the bug here.

  4. Jerrody commented on Oct 31, 2022

    @Jerrody
    ContributorAuthor

    Do you mind sharing what the suggested fix actually was? It's hard to tell what was applied just from the bug here.

    Okay, conn has a type String. It's suggested to remove a reference and pass a value, but it didn't understand the context that this happens in a loop.
    So, an error happens that was used move value.

  5. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 31, 2022
  6. estebank commented on Oct 31, 2022

    @estebank
    Contributor

    FWIW, be aware that diagnostics don't always have enough information available to them to fully provide correct code, but we consider it appropriate if subsequent compilations provide enough guidance that you can eventually end up with correct code. Having said that, we absolutely appreciate reports of cases like these where we can preclude some of those iterations.

    Here, the type error suggestion can/should climb up from the current expression until it finds the binding creation and track whether any looping expression is in between them. If so, it should suggest both removing the & and a .clone() call.

  7. Jerrody commented on Oct 31, 2022

    @Jerrody
    ContributorAuthor

    FWIW, be aware that diagnostics don't always have enough information available to them to fully provide correct code, but we consider it appropriate if subsequent compilations provide enough guidance that you can eventually end up with correct code. Having said that, we absolutely appreciate reports of cases like these where we can preclude some of those iterations.

    Here, the type error suggestion can/should climb up from the current expression until it finds the binding creation and track whether any looping expression is in between them. If so, it should suggest both removing the & and a .clone() call.

    I'm happy that my issue is helpful. I'm not a lang dev, but I think that compiler doesn't provide a context that this happens in the loop, just ignored this fact.
    Anyway, thanks for the positive feedback, have a good day or evening!

  8. estebank commented on Nov 3, 2022

    @estebank
    Contributor

    #103908 will provide the following error after the first applied change:

    error[E0382]: use of moved value: `conn`
      --> src\cli\sql.rs:43:12
       |
    22 |     let conn = format!("{conn}/sql");
       |         ---- move occurs because `conn` has type `std::string::String`, which does not implement the `Copy` trait
    ...
    28 | 	 loop {
       |     ---- inside of this loop
    ...
    43 |                     .post(conn)
       |                           ^^^^ value moved here, in previous iteration of loop
    help: consider cloning the value if the performance cost is acceptable
       |
    43 |                     .post(conn.clone())
       |                               ++++++++
    
  9. changed the title [-]BUG: `failed to automatically apply fixes suggested by rustc` due to `use of moved value: conn`.[/-] [+]`failed to automatically apply fixes suggested by rustc` due to `use of moved value`[/+] on Nov 3, 2022
  10. Jerrody commented on Nov 25, 2022

    @Jerrody
    ContributorAuthor

    Fixed: #103908.

    I close this issue, anyway I can reopen it due to appeared issues after this PR.

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-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions