Repository: intellij-rust/intellij-rust
Issue: Cannot open DTrace profiler report in CLion 2021.2

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.155.SNAPSHOT-212-dev
* **Rust toolchain version:** 1.54.0 (a178d0322 2021-07-26) x86_64-apple-darwin
* **IDE name and version:** CLion 2021.2 (CL-212.5080.2)
* **Operating system:** macOS 11.2
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description
Original issue in YT: https://youtrack.jetbrains.com/issue/CPP-26164
Only affects profiling on macOS with CLion since 2021.2.

## Steps to reproduce
1. Profile any Cargo run configuration
2. Click `Open` after profiling report is done or open `Profiler` tool window
3. CLion does not show profiling report and `java.lang.UnsupportedOperationException: Empty collection can't be reduced` exception is thrown

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->

Changed files:
clion/src/main/kotlin/org/rust/clion/profiler/dtrace/RsDTraceProfilerProcess.kt

diff --git a/clion/src/main/kotlin/org/rust/clion/profiler/dtrace/RsDTraceProfilerProcess.kt b/clion/src/main/kotlin/org/rust/clion/profiler/dtrace/RsDTraceProfilerProcess.kt
index 1c818128c..a19fe2ace 100644
--- a/clion/src/main/kotlin/org/rust/clion/profiler/dtrace/RsDTraceProfilerProcess.kt
+++ b/clion/src/main/kotlin/org/rust/clion/profiler/dtrace/RsDTraceProfilerProcess.kt
@@ -5,8 +5,10 @@
 
 package org.rust.clion.profiler.dtrace
 
+import com.intellij.openapi.application.ApplicationInfo
 import com.intellij.openapi.progress.PerformInBackgroundOption
 import com.intellij.openapi.project.Project
+import com.intellij.openapi.util.BuildNumber
 import com.intellij.profiler.DummyCallTreeBuilder
 import com.intellij.profiler.api.*
 import com.intellij.profiler.dtrace.DTraceProfilerProcessBase
@@ -38,7 +40,12 @@ class RsDTraceProfilerProcess private constructor(
 
     override fun createDumpParser(): FullDumpParser<BaseCallStackElement> {
         val cachingStackElementReader = RsCachingStackElementReader.getInstance(project)
-        return FullDumpParser(NativeThread.Companion::fromId, cachingStackElementReader::parseStackElement)
+        // BACKCOMPAT: 2021.1. Since 2021.2, CLion does not show the thread id, so it should be specified as a thread name
+        val shouldSpecifyThreadName = ApplicationInfo.getInstance().build >= BUILD_212
+        return FullDumpParser(
+            { if (shouldSpecifyThreadName) NativeThread(it, "thread with id $it") else NativeThread(it, "") },
+            cachingStackElementReader::parseStackElement
+        )
     }
 
     override fun createDumpWriter(data: NewCallTreeOnlyProfilerData): ProfilerDumpWriter =
@@ -86,3 +93,6 @@ class RsDTraceProfilerProcess private constructor(
         }
     }
 }
+
+// BACKCOMPAT: 2021.1
+private val BUILD_212: BuildNumber = BuildNumber.fromString("212")!!