【发布时间】:2021-04-08 15:33:44
【问题描述】:
我有一段奇怪的代码:
#![allow(unused)]
fn f<'a>() {}
fn g<'a: 'a>() {}
fn main() {
// let pf = f::<'static> as fn(); // (7)
let pg = g::<'static> as fn(); // (8)
//print!("{}", pf == pg);
}
第 7 行不加注释无法编译(如下错误),但第 8 行可以编译。
error: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
--> src/main.rs:7:18
|
7 | let pf = f::<'static> as fn(); // (7)
| ^^^^^^^
|
note: the late bound lifetime parameter is introduced here
--> src/main.rs:3:6
|
3 | fn f<'a>() {}
| ^^
第4行'a: 'a是什么意思?
【问题讨论】:
-
我能找到的
f::<'static>中错误的唯一引用是in the rusc dev guide。看起来<'a>是一个后期绑定参数,但<'a: 'a>是一个早期绑定参数,明确指定后期绑定参数是非法的。我不确定为什么,但它可能值得在rust-internals 发帖,如果只是因为错误消息非常粗略并且可能有关于如何清理它的想法。 -
如果在函数参数中实际使用了生命周期,both 会出现错误。这个简化的示例不太可能出现在“真实”代码中。确切的错误消息似乎是与 GAT 工作相关的更改的副作用。
-
您是否阅读了 dtolnay 网站上的问题描述?特别是“根据这些规则,签名
fn f<'a>()具有后期绑定生命周期参数,而签名fn g<'a: 'a>()具有早期绑定生命周期参数——即使此处的约束无效。”