如何在不看代码的情况下读取生命周期错误?

How to read a lifetime error without looking at the code?

我收到以下生命周期错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> prusti-viper/src/procedures_table.rs:42:40
   |
42 |         let mut cfg = self.cfg_factory.new_cfg_method(
   |                                        ^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 40:5...
  --> prusti-viper/src/procedures_table.rs:40:5
   |
40 | /     pub fn set_used(&mut self, proc_def_id: ProcedureDefId) {
41 | |         let procedure = self.env.get_procedure(proc_def_id);
42 | |         let mut cfg = self.cfg_factory.new_cfg_method(
43 | |             // method name
...  |
135| |         self.procedures.insert(proc_def_id, method);
136| |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> prusti-viper/src/procedures_table.rs:42:23
   |
42 |         let mut cfg = self.cfg_factory.new_cfg_method(
   |                       ^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'v as defined on the impl at 22:1...
  --> prusti-viper/src/procedures_table.rs:22:1
   |
22 | impl<'v, P: Procedure> ProceduresTable<'v, P> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...so that the expression is assignable:
           expected viper::Method<'v>
              found viper::Method<'_>

error: aborting due to previous error

不看代码,只看报错信息,是否可以理解报错信息指的是哪个lifetimes/references/borrows?这是用我的问题注释的消息:

error[E0495]: cannot infer an appropriate lifetime for autoref (what is autoref?) due to conflicting requirements

note: first, the lifetime (which lifetime?) cannot outlive the anonymous lifetime #1 (the one of &mut self, ok) defined on the method body at 40:5...

...so that reference (which reference?) does not outlive borrowed content (which borrowed content?)

but, the lifetime must be valid for the lifetime 'v as defined on the impl at 22:1... (why these constraints?)

例如,我正在寻找类似 "In error message E0495 the lifetime that cannot outlive the anonymous lifetime #1 is always the lifetime of self, in other words #1 again" 的解释。

通过查看类似问题 (, , ) 的现有答案,我找不到对错误消息所指内容的解释。有时答案只是写 "in this case the lifetime is 'a",但我想知道如何理解它是 'a 而不是其他 'b。其他时候答案涉及对源代码的推理,但这对我来说是以下步骤之一:首先阅读消息并理解它所指的内容,然后理解错误(在这种情况下,可能与生命周期要求相冲突) ,然后查看代码并尝试修复错误。

cannot infer an appropriate lifetime for autoref due to conflicting requirements

这是错误的关键部分。一生有两个(或更多)要求,而且它们相互冲突。 "autoref" 表示通过调用采用 &self 的方法所采用的引用。引用的代码行表明哪个方法调用是错误的。

note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 40:5

这是第一个冲突的要求。 "The lifetime" 表示第一条消息中提到的那个:它试图推断的那个,对于这个 autoref。它不能比您调用该方法的对象长寿。引用的代码表示该对象的生命周期。在这种情况下,生命周期是错误行所在的整个方法,因为您借用的对象是该方法的 &mut self.

的成员

note: ...so that reference does not outlive borrowed content

这只是进一步解释了这一点。 "That reference" - 您尝试获取的 autoref - 不能比 &mut self 所指的对象长寿。

note: but, the lifetime must be valid for the lifetime 'v as defined on the impl at 22:1...

这里"but"引入第二个需求,与第一个需求冲突

你问"why these constraints?",编译器马上解释:

note: ...so that the expression is assignable:
expected viper::Method<'v>
found viper::Method<'_>

有问题的作业是错误行中的作业。您正在将 new_cfg_method 的结果分配给 cfg。 "expected" 是赋值 cfg 的左边 hand-side,它的类型必须是 viper::Method<'v>。 "found" 是 right-hand 端,方法调用的结果,其类型为 viper::Method<'_>'_ 表示编译器试图推断的生命周期。也就是说,这是您随后使用 cfg 的方式,这意味着它必须具有生命周期 'v。为什么会这样取决于错误消息中未引用的代码。

要解决此问题,您需要执行以下操作之一:

  1. 去掉第一个要求。更改 new_cfg_method 以使其结果的生命周期与调用它的对象的生命周期无关:可能通过删除它包含的一些引用。
  2. 去掉第二个要求。更改使用 cfg 的代码,使其不需要具有生命周期 'v。同样,这可以通过删除 viper::Method.
  3. 中的一些引用来实现

如果两者都做不到,您可能需要引入 Cell 或其他东西来动态而不是静态地管理生命周期。