Why do I get "generic parameters may not be used in const operations" error when compiling dependencies?

Viewed 66

I'm trying to compile the sample code from the docx crate:

# Cargo.toml
[dependencies]
docx = "1.1.2"
//! main.rs
use docx::document::Paragraph;
use docx::DocxFile;

fn main() {
    let docx = DocxFile::from_file("origin.docx").unwrap();
    let mut docx = docx.parse().unwrap();

    let para = Paragraph::default().push_text("Lorem Ipsum");
    docx.document.push(para);

    docx.write_file("origin_appended.docx").unwrap();
}

This is the full error I'm getting:

   Compiling bzip2-sys v0.1.11+1.0.8
   Compiling jetscii v0.4.4
   Compiling quote v1.0.21
   Compiling time v0.1.44
error: generic parameters may not be used in const operations
   --> /home/thwart/.cargo/registry/src/github.com-1ecc6299db9ec823/jetscii-0.4.4/src/simd.rs:109:13
    |
109 |             T::CONTROL_BYTE,
    |             ^^^^^^^^^^^^^^^ cannot perform const operation using `T`
    |
    = note: type parameters may not be used in const expressions

error: generic parameters may not be used in const operations
   --> /home/thwart/.cargo/registry/src/github.com-1ecc6299db9ec823/jetscii-0.4.4/src/simd.rs:148:13
    |
148 |             T::CONTROL_BYTE,
    |             ^^^^^^^^^^^^^^^ cannot perform const operation using `T`
    |
    = note: type parameters may not be used in const expressions

error: could not compile `jetscii` due to 2 previous errors

Why is Rust compiling jetscii? How do I fix this error?

1 Answers

This is caused by an unfortunate combination of a slightly-incompatible Rust update and old crates. The docx library depends on jetscii via this dependency tree:

docx v1.1.2
└── strong-xml v0.5.0
    └── jetscii v0.4.4

You can view jetscii issue #56, jetscii pull request #39, and rust-lang/stdarch issue #248 for more info, but the basic summary is (courtesy of shepmaster):

TL;DR, older versions of Rust exposed the SIMD intrinsics in one way, that was changed, and a small number of crates (such as Jetscii) were affected.

The jetscii and strong-xml have long since been updated to accommodate this change but docx has not (no updates in over two years).

To workaround this issue your options are limited:

  • use Rust version 1.53
  • update docx with newer dependencies yourself
  • change libraries (consider docx-rs or docx-rust)
Related