Skip to content

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

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

Closed
Jerrody opened this issue Oct 30, 2022 · 6 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Jerrody
Copy link
Contributor

Jerrody commented Oct 30, 2022

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.

@Jerrody Jerrody added the C-bug Category: This is a bug. label Oct 30, 2022
@Jerrody Jerrody 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`. Oct 30, 2022
@Jerrody Jerrody 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. Oct 30, 2022
@compiler-errors
Copy link
Member

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

@Jerrody
Copy link
Contributor Author

Jerrody commented Oct 31, 2022

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.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 31, 2022
@estebank
Copy link
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.

@Jerrody
Copy link
Contributor Author

Jerrody commented Oct 31, 2022

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!

@estebank
Copy link
Contributor

estebank commented Nov 3, 2022

#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())
   |                               ++++++++

@estebank estebank 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 Nov 3, 2022
@Jerrody
Copy link
Contributor Author

Jerrody commented Nov 25, 2022

Fixed: #103908.

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

@Jerrody Jerrody closed this as completed Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

3 participants