intellij-rust: false positives when detecting errors in editor

Environment

  • IntelliJ Rust plugin version: 0.4.179.4903-222
  • Rust toolchain version: 1.64.0 (a55dd71d5 2022-09-19) x86_64-pc-windows-msvc
  • IDE name and version: IntelliJ IDEA 2022.2.2 Ultimate Edition (IU-222.4167.29)
  • Operating system: Windows 10 10.0
  • Macro expansion: enabled

Problem description

I get a lot of underwaved errors when editing Rust code, for

  • formatted strings, e.g. println! and similar macros, with the following explanation: "‘(type)’ doesn’t implement ‘Display’, for types like String, usize, …
  • arguments of generic functions with types that should normally be accepted, but are marked as wrong.

The program compiles without any error or warning, and runs without any problem.

A few examples:

  • ‘usize’ doesn’t implement ‘Display’: image

  • ‘String’ doesn’t implement ‘Display’: (the tooltip only shows the function signature, not the error) image

  • here, a function that takes any type from which f64 can be taken: image

  • (function signature) image

This makes IntelliJ’s error detection unusable and is very annoying.

Misc info

Some of the errors disappear if I invalidate the cache and restart the IDE (File, Invalidate Caches…), but not all of them. Then they come back after a while.

I never saw that in 2022.2.1. I think I briefly used it with 1.64 and never saw those problems either. I have just updated to 2022.2.2 and now I’m seeing them all the time.

EDIT: work-around by reverting to previous version, see post below.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 27 (8 by maintainers)

Commits related to this issue

Most upvoted comments

It was difficult, but I solved this riddle! The bug was introduced in #9229 (included into 0.4.179 release). I’m fixing it in #9461 that will likely be included into 0.4.180 (will be released next Monday).

Technical explanation: CargoBasedCrate had identity-based equals/hashCode (looking ahead, this led to the bug). CrateGraphServiceImpl.crateGraph is stored under a CachedValue that stores a cached value indirectly under SoftReference (“indirectly” means SoftReference -> SomeWrapper -> CrateGraph), so Java GC can collect SomeWrapper anytime, that means invalidation of the the crateGraph cached value (and its re-calculation during a next access). Hence it’s possible that at some point we have multiple CargoBasedCrate instances referring to the “same” crate. Crate.hasTransitiveDependencyOrSelf(Crate) (introduced in #9229) uses CargoBasedCrate.equals/hashCode in order to check if a crate is a dependency of another crate. The combination of these nuances has led to the fact that in some circumstances Crate.hasTransitiveDependencyOrSelf(Crate) could wrongly return false, that turns the #9229 filtering mechanism against valid impls (like impl Copy for primitives), that in turn leads to observable false-positives

The issue is fixed in the nightly plugin. You can try it now (see instructions)

Thank everyone for participating!

So far I haven’t seen the problem since I reverted to Rust 0.4.178.4873-222, so I would say it’s a good work around.

I think those type-related optimizations above may have broken something in the trait system in version 0.4.179. It seems unable to see what traits are supported by a type.

Procedure:

  • Visit this page and download the appropriate 0.4.178 version depending on your IntelliJ version: https://plugins.jetbrains.com/plugin/8182-rust/versions/stable
  • Open the settings and the plugin section.
  • Click on the setting cog wheel at the top, NOT the one in the plugin information area (see below).
  • Select Install Plugin from disk.

No need to uninstall it first. Maybe a Restart & Invalidate Caches may be safer, though I didn’t need to do it.

image

It was difficult, but I solved this riddle! The bug was introduced in #9229 (included into 0.4.179 release). I’m fixing it in #9461 that will likely be included into 0.4.180 (will be released next Monday).

Technical explanation: CargoBasedCrate had identity-based equals/hashCode (looking ahead, this led to the bug). CrateGraphServiceImpl.crateGraph is stored under a CachedValue that stores a cached value indirectly under SoftReference (“indirectly” means SoftReference -> SomeWrapper -> CrateGraph), so Java GC can collect SomeWrapper anytime, that means invalidation of the the crateGraph cached value (and its re-calculation during a next access). Hence it’s possible that at some point we have multiple CargoBasedCrate instances referring to the “same” crate. Crate.hasTransitiveDependencyOrSelf(Crate) (introduced in #9229) uses CargoBasedCrate.equals/hashCode in order to check if a crate is a dependency of another crate. The combination of these nuances has led to the fact that in some circumstances Crate.hasTransitiveDependencyOrSelf(Crate) could wrongly return false that turns the #9229 filtering mechanism against valid impls (like impl Copy for primitives) that in turn leads to observable false-positives

Remarkable! Great work!

@blueglyph you can do it via https://uploads.jetbrains.com not to make them publicly available