编译器错误消息中的{integer}或{float}是什么? [英] What is the {integer} or {float} in a compiler error message?

查看:78
本文介绍了编译器错误消息中的{integer}或{float}是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中很难找到它。这甚至可能是一个分为两个部分的问题:

It is surprisingly hard to find this in the docs. This might even be a two part question:


  1. Are {integer} {float} 特定原始类型的某种语言别名?

  1. Are {integer} and {float} some sort of language alias for a specific primitive type?

这是什么?在
a编译/语法错误消息中将类型名称括在花括号中的意思是什么?

What does it mean for a type name to be enclosed in curly braces in a compilation/syntax error message?

示例:


错误:找不到类型为 pow 的方法> {integer} 在当前
范围内

error: no method named pow found for type {integer} in the current scope


推荐答案

{integer} 是一个整数值,其具体类型尚未指定,并且尚未由编译器推断;以下代码:

{integer} is an integral value whose concrete type was not specified and has not been inferred by the compiler yet; the following code:

fn main() {
    let x = 1;
    let () = x;
}

将导致以下错误:

error[E0308]: mismatched types

 --> <anon>:3:9
  |
3 |     let () = x;
  |         ^^ expected integral variable, found ()
  |
  = note: expected type `{integer}`
  = note:    found type `()`

使用浮点数会发生同样的情况:

The same would happen with a floating number:

fn main() {
    let x = 1.0;
    let () = x;
}



error[E0308]: mismatched types
 --> <anon>:3:9
  |
3 |     let () = x;
  |         ^^ expected floating-point variable, found ()
  |
  = note: expected type `{float}`
  = note:    found type `()`

由于类型分配无效之前引发了由无效赋值 let()= x 引起的编译错误。

Because the compilation error caused by the invalid assignment let () = x is thrown before the type inference can happen.

换句话说,直到编译到达类型推断阶段,在该阶段,将识别(例如,基于函数应用程序的)没有指定具体类型的整数或浮点数或为其指定默认类型, i32 表示整数, f64 表示浮点数,编译错误会将其称为 {integer} {float}

In other words, until the compilation reaches the type inference stage where an integer or a float without a concrete type specified would be recognized (e.g. based on function application) or assigned the default type, i32 for integers and f64 for floats, compilation errors will refer to it as an {integer} or a {float}.

这篇关于编译器错误消息中的{integer}或{float}是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆