diff options
| author | bors <bors@rust-lang.org> | 2025-02-19 04:37:26 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-02-19 04:37:26 +0000 |
| commit | 5986ff05d8480da038dd161b3a6aa79ff364a851 (patch) | |
| tree | 1a1274cb0be9cf42d367279eef79156f5ebe2678 /compiler/rustc_error_codes/src | |
| parent | 17c1c329a5512d718b67ef6797538b154016cd34 (diff) | |
| parent | 24ba1ad31ceb5537bb03bedfb6aac888b7b313b9 (diff) | |
| download | rust-5986ff05d8480da038dd161b3a6aa79ff364a851.tar.gz rust-5986ff05d8480da038dd161b3a6aa79ff364a851.zip | |
Auto merge of #137248 - matthiaskrgr:rollup-s18zjau, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #136936 (Use 'yes' instead of 'while-echo' in tests/ui/process/process-sigpipe.rs except 'nto') - #137026 (Stabilize (and const-stabilize) `integer_sign_cast`) - #137059 (fix: Alloc new errorcode E0803 for E0495) - #137177 (Update `minifier-rs` version to `0.3.5`) - #137210 (compiler: Stop reexporting stuff in cg_llvm::abi) - #137213 (Remove `rustc_middle::mir::tcx` module.) - #137216 (eval_outlives: bail out early if both regions are in the same SCC) - #137228 (Fix typo in hidden internal docs of `TrustedRandomAccess`) - #137242 (Add reference annotations for the `do_not_recommend` attribute) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_error_codes/src')
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0803.md | 46 | ||||
| -rw-r--r-- | compiler/rustc_error_codes/src/lib.rs | 1 |
2 files changed, 47 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0803.md b/compiler/rustc_error_codes/src/error_codes/E0803.md new file mode 100644 index 00000000000..4c022688a2d --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0803.md @@ -0,0 +1,46 @@ +A trait implementation returns a reference without an +explicit lifetime linking it to `self`. +It commonly arises in generic trait implementations +requiring explicit lifetime bounds. + +Erroneous code example: + +```compile_fail,E0803 +trait DataAccess<T> { + fn get_ref(&self) -> T; +} + +struct Container<'a> { + value: &'a f64, +} + +// Attempting to implement reference return +impl<'a> DataAccess<&f64> for Container<'a> { + fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch + self.value + } +} +``` + +The trait method returns &f64 requiring an independent lifetime +The struct Container<'a> carries lifetime parameter 'a +The compiler cannot verify if the returned reference satisfies 'a constraints +Solution +Explicitly bind lifetimes to clarify constraints: +``` +// Modified trait with explicit lifetime binding +trait DataAccess<'a, T> { + fn get_ref(&'a self) -> T; +} + +struct Container<'a> { + value: &'a f64, +} + +// Correct implementation (bound lifetimes) +impl<'a> DataAccess<'a, &'a f64> for Container<'a> { + fn get_ref(&'a self) -> &'a f64 { + self.value + } +} +``` diff --git a/compiler/rustc_error_codes/src/lib.rs b/compiler/rustc_error_codes/src/lib.rs index e970b16f610..098ca42be2b 100644 --- a/compiler/rustc_error_codes/src/lib.rs +++ b/compiler/rustc_error_codes/src/lib.rs @@ -546,6 +546,7 @@ E0799: 0799, E0800: 0800, E0801: 0801, E0802: 0802, +E0803: 0803, ); ) } |
