Skip to content

Adding struct/variant causes inference error in unrelated code #80964

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
wchargin opened this issue Jan 13, 2021 · 4 comments
Closed

Adding struct/variant causes inference error in unrelated code #80964

wchargin opened this issue Jan 13, 2021 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@wchargin
Copy link
Contributor

wchargin commented Jan 13, 2021

I tried this code:

//! Stripped-down repro case for a strange bug, wherein adding a new struct or enum variant can
//! surface type inference errors in completely unrelated code, even in different modules.

mod pb {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
    #[repr(i32)]
    pub enum DataClass {
        Unknown = 0,
        Scalar = 1,
        Tensor = 2,
        BlobSequence = 3,
    }
    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct SummaryMetadata {
        #[prost(enumeration = "DataClass", tag = "4")]
        pub data_class: i32,
    }
}

pub fn test() {
    let md = pb::SummaryMetadata::default();
    assert_eq!(md.data_class, pb::DataClass::BlobSequence.into());
}

// Uncomment this line to create an inference error at line 19:
//pub struct Foo(serde_json::Error);

I expected it to compile, and it does. But when I uncomment the final
line, a new inference error appears on the assert_eq! line:

error[E0282]: type annotations needed
  --> src/lib.rs:22:59
   |
22 |     assert_eq!(md.data_class, pb::DataClass::BlobSequence.into());
   |                               ----------------------------^^^^--
   |                               |                           |
   |                               |                           cannot infer type for type parameter `T` declared on the trait `Into`
   |                               this method call resolves to `T`

By itself, the inference error seems reasonable enough, and I can just
add type annotations. But the change has nothing to do with the error.

This is a stripped-down example. In my real code, I’m adding a variant
to an existing error enum, and I see two inference errors in entirely
unrelated modules:

$ git diff -U1
diff --git a/tensorboard/data/server/lib.rs b/tensorboard/data/server/lib.rs
index f1def3891..357693441 100644
--- a/tensorboard/data/server/lib.rs
+++ b/tensorboard/data/server/lib.rs
@@ -19,2 +19,4 @@ limitations under the License.
 
+pub struct Foo(serde_json::Error);
+
 pub mod cli;

$ cargo check --tests
error[E0282]: type annotations needed
   --> data_compat.rs:456:67
    |
456 |             assert_eq!(md.data_class, pb::DataClass::BlobSequence.into());
    |                                       ----------------------------^^^^--
    |                                       |                           |
    |                                       |                           cannot infer type for type parameter `T` declared on the trait `Into`
    |                                       this method call resolves to `T`

error[E0282]: type annotations needed
   --> server.rs:629:37
    |
629 |         assert_eq!(xent_data.value, Vec::new());
    |                                     ^^^^^^^^ cannot infer type for type parameter `T`

Cloneable example:
https://github.com/wchargin/rust-inference-action-at-a-distance

Meta

Can repro in 1.48.0, 1.49.0-beta, and 2021-01-06 nightly.

cargo/rustc versions
$ cargo --version --verbose
cargo 1.48.0 (65cbdd2dc 2020-10-14)
release: 1.48.0
commit-hash: 65cbdd2dc0b7e877577474b98b7d071308d0bb6f
commit-date: 2020-10-14

$ cargo +beta --version --verbose
cargo 1.49.0-beta (8662ab427 2020-11-12)
release: 1.49.0
commit-hash: 8662ab427a8d6ad8047811cc4d78dbd20dd07699
commit-date: 2020-11-12

$ cargo +nightly --version --verbose
cargo 1.51.0-nightly (329895f5b 2021-01-06)
release: 1.51.0
commit-hash: 329895f5b52a358e5d9ecb26215708b5cb31d906
commit-date: 2021-01-06
Cargo.toml
[package]
name = "inference-action-at-a-distance"
version = "0.1.0"
edition = "2018"

[dependencies]
prost = "0.6.1"
serde_json = "1.0.59"
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "anyhow"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"

[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"

[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"

[[package]]
name = "inference-action-at-a-distance"
version = "0.1.0"
dependencies = [
 "prost",
 "serde_json",
]

[[package]]
name = "itertools"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
dependencies = [
 "either",
]

[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"

[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
 "unicode-xid",
]

[[package]]
name = "prost"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce49aefe0a6144a45de32927c77bd2859a5f7677b55f220ae5b744e87389c212"
dependencies = [
 "bytes",
 "prost-derive",
]

[[package]]
name = "prost-derive"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537aa19b95acde10a12fec4301466386f757403de4cd4e5b4fa78fb5ecb18f72"
dependencies = [
 "anyhow",
 "itertools",
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "quote"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"

[[package]]
name = "serde"
version = "1.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3"

[[package]]
name = "serde_json"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a"
dependencies = [
 "itoa",
 "ryu",
 "serde",
]

[[package]]
name = "syn"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-xid",
]

[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
@wchargin wchargin added the C-bug Category: This is a bug. label Jan 13, 2021
@wchargin
Copy link
Contributor Author

Perhaps related: #44345? Mine doesn’t involve a dep upgrade, though.

@jonas-schievink
Copy link
Contributor

Presumably this happens when serde_json is loaded, so use serde_json as _; instead of the struct should also cause this

@wchargin
Copy link
Contributor Author

Indeed, it does.

@jonas-schievink
Copy link
Contributor

In that case, closing as a duplicate of #44345

wchargin added a commit to tensorflow/tensorboard that referenced this issue Jan 13, 2021
Summary:
The data provider RPC protocol requires servers to emit “blob keys”,
opaque, URL-safe tokens that uniquely identify blobs. This patch defines
an in-memory `BlobKey` struct with encoding and decoding. The wire
format is URL-safe base64 (with no padding) over JSON.

The seemingly unrelated changes are needed because loading `serde_json`
adds trait implementations that make some type inference inadmissible:
<rust-lang/rust#80964>

Test Plan:
Unit tests included.

wchargin-branch: rust-blob-keys
wchargin-source: bd9d325a73fb7757b2916e52a72c4e23255c51ed
wchargin added a commit to tensorflow/tensorboard that referenced this issue Jan 13, 2021
Summary:
The data provider RPC protocol requires servers to emit “blob keys”,
opaque, URL-safe tokens that uniquely identify blobs. This patch defines
an in-memory `BlobKey` struct with encoding and decoding. The wire
format is URL-safe base64 (with no padding) over JSON.

The seemingly unrelated changes are needed because loading `serde_json`
adds trait implementations that make some type inference inadmissible:
<rust-lang/rust#80964>

Test Plan:
Unit tests included.

wchargin-branch: rust-blob-keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants