/src/test/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs

https://gitlab.com/rust-lang/rust · Rust · 501 lines · 385 code · 52 blank · 64 comment · 0 complexity · a7d9181f135c019abc258225a082610c MD5 · raw file

  1. // check-fail
  2. // Tests error conditions for specifying subdiagnostics using #[derive(SessionSubdiagnostic)]
  3. // The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
  4. // changing the output of this test. Since SessionSubdiagnostic is strictly internal to the compiler
  5. // the test is just ignored on stable and beta:
  6. // ignore-beta
  7. // ignore-stable
  8. #![feature(rustc_private)]
  9. #![crate_type = "lib"]
  10. extern crate rustc_errors;
  11. extern crate rustc_session;
  12. extern crate rustc_span;
  13. extern crate rustc_macros;
  14. use rustc_errors::Applicability;
  15. use rustc_span::Span;
  16. use rustc_macros::SessionSubdiagnostic;
  17. #[derive(SessionSubdiagnostic)]
  18. #[label(slug = "label-a")]
  19. struct A {
  20. #[primary_span]
  21. span: Span,
  22. var: String,
  23. }
  24. #[derive(SessionSubdiagnostic)]
  25. enum B {
  26. #[label(slug = "label-b-a")]
  27. A {
  28. #[primary_span]
  29. span: Span,
  30. var: String,
  31. },
  32. #[label(slug = "label-b-b")]
  33. B {
  34. #[primary_span]
  35. span: Span,
  36. var: String,
  37. }
  38. }
  39. #[derive(SessionSubdiagnostic)]
  40. #[label(slug = "label-c")]
  41. //~^ ERROR label without `#[primary_span]` field
  42. struct C {
  43. var: String,
  44. }
  45. #[derive(SessionSubdiagnostic)]
  46. #[label]
  47. //~^ ERROR `#[label]` is not a valid attribute
  48. struct D {
  49. #[primary_span]
  50. span: Span,
  51. var: String,
  52. }
  53. #[derive(SessionSubdiagnostic)]
  54. #[foo]
  55. //~^ ERROR `#[foo]` is not a valid attribute
  56. //~^^ ERROR cannot find attribute `foo` in this scope
  57. struct E {
  58. #[primary_span]
  59. span: Span,
  60. var: String,
  61. }
  62. #[derive(SessionSubdiagnostic)]
  63. #[label = "..."]
  64. //~^ ERROR `#[label = ...]` is not a valid attribute
  65. struct F {
  66. #[primary_span]
  67. span: Span,
  68. var: String,
  69. }
  70. #[derive(SessionSubdiagnostic)]
  71. #[label(bug = "...")]
  72. //~^ ERROR `#[label(bug = ...)]` is not a valid attribute
  73. struct G {
  74. #[primary_span]
  75. span: Span,
  76. var: String,
  77. }
  78. #[derive(SessionSubdiagnostic)]
  79. #[label("...")]
  80. //~^ ERROR `#[label("...")]` is not a valid attribute
  81. struct H {
  82. #[primary_span]
  83. span: Span,
  84. var: String,
  85. }
  86. #[derive(SessionSubdiagnostic)]
  87. #[label(slug = 4)]
  88. //~^ ERROR `#[label(slug = ...)]` is not a valid attribute
  89. struct J {
  90. #[primary_span]
  91. span: Span,
  92. var: String,
  93. }
  94. #[derive(SessionSubdiagnostic)]
  95. #[label(slug("..."))]
  96. //~^ ERROR `#[label(slug(...))]` is not a valid attribute
  97. struct K {
  98. #[primary_span]
  99. span: Span,
  100. var: String,
  101. }
  102. #[derive(SessionSubdiagnostic)]
  103. #[label(slug)]
  104. //~^ ERROR `#[label(slug)]` is not a valid attribute
  105. struct L {
  106. #[primary_span]
  107. span: Span,
  108. var: String,
  109. }
  110. #[derive(SessionSubdiagnostic)]
  111. #[label()]
  112. //~^ ERROR `slug` must be set in a `#[label(...)]` attribute
  113. struct M {
  114. #[primary_span]
  115. span: Span,
  116. var: String,
  117. }
  118. #[derive(SessionSubdiagnostic)]
  119. #[label(code = "...")]
  120. //~^ ERROR `code` is not a valid nested attribute of a `label` attribute
  121. struct N {
  122. #[primary_span]
  123. span: Span,
  124. var: String,
  125. }
  126. #[derive(SessionSubdiagnostic)]
  127. #[foo]
  128. //~^ ERROR cannot find attribute `foo` in this scope
  129. //~^^ ERROR unsupported type attribute for subdiagnostic enum
  130. enum O {
  131. #[label(slug = "...")]
  132. A {
  133. #[primary_span]
  134. span: Span,
  135. var: String,
  136. }
  137. }
  138. #[derive(SessionSubdiagnostic)]
  139. enum P {
  140. #[bar]
  141. //~^ ERROR `#[bar]` is not a valid attribute
  142. //~^^ ERROR cannot find attribute `bar` in this scope
  143. A {
  144. #[primary_span]
  145. span: Span,
  146. var: String,
  147. }
  148. }
  149. #[derive(SessionSubdiagnostic)]
  150. enum Q {
  151. #[bar = "..."]
  152. //~^ ERROR `#[bar = ...]` is not a valid attribute
  153. //~^^ ERROR cannot find attribute `bar` in this scope
  154. A {
  155. #[primary_span]
  156. span: Span,
  157. var: String,
  158. }
  159. }
  160. #[derive(SessionSubdiagnostic)]
  161. enum R {
  162. #[bar = 4]
  163. //~^ ERROR `#[bar = ...]` is not a valid attribute
  164. //~^^ ERROR cannot find attribute `bar` in this scope
  165. A {
  166. #[primary_span]
  167. span: Span,
  168. var: String,
  169. }
  170. }
  171. #[derive(SessionSubdiagnostic)]
  172. enum S {
  173. #[bar("...")]
  174. //~^ ERROR `#[bar("...")]` is not a valid attribute
  175. //~^^ ERROR cannot find attribute `bar` in this scope
  176. A {
  177. #[primary_span]
  178. span: Span,
  179. var: String,
  180. }
  181. }
  182. #[derive(SessionSubdiagnostic)]
  183. enum T {
  184. #[label(code = "...")]
  185. //~^ ERROR `code` is not a valid nested attribute of a `label`
  186. A {
  187. #[primary_span]
  188. span: Span,
  189. var: String,
  190. }
  191. }
  192. #[derive(SessionSubdiagnostic)]
  193. enum U {
  194. #[label(slug = "label-u")]
  195. A {
  196. #[primary_span]
  197. span: Span,
  198. var: String,
  199. },
  200. B {
  201. //~^ ERROR subdiagnostic kind not specified
  202. #[primary_span]
  203. span: Span,
  204. var: String,
  205. }
  206. }
  207. #[derive(SessionSubdiagnostic)]
  208. #[label(slug = "...")]
  209. //~^ ERROR label without `#[primary_span]` field
  210. struct V {
  211. #[primary_span]
  212. //~^ ERROR the `#[primary_span]` attribute can only be applied to fields of type `Span`
  213. span: String,
  214. }
  215. #[derive(SessionSubdiagnostic)]
  216. #[label(slug = "...")]
  217. struct W {
  218. #[primary_span]
  219. span: Span,
  220. #[applicability]
  221. //~^ ERROR `#[applicability]` is only valid on suggestions
  222. applicability: Applicability,
  223. }
  224. #[derive(SessionSubdiagnostic)]
  225. #[label(slug = "...")]
  226. struct X {
  227. #[primary_span]
  228. span: Span,
  229. #[bar]
  230. //~^ ERROR `#[bar]` is not a valid attribute
  231. //~^^ ERROR cannot find attribute `bar` in this scope
  232. bar: String,
  233. }
  234. #[derive(SessionSubdiagnostic)]
  235. #[label(slug = "...")]
  236. struct Y {
  237. #[primary_span]
  238. span: Span,
  239. #[bar = "..."]
  240. //~^ ERROR `#[bar = ...]` is not a valid attribute
  241. //~^^ ERROR cannot find attribute `bar` in this scope
  242. bar: String,
  243. }
  244. #[derive(SessionSubdiagnostic)]
  245. #[label(slug = "...")]
  246. struct Z {
  247. #[primary_span]
  248. span: Span,
  249. #[bar("...")]
  250. //~^ ERROR `#[bar(...)]` is not a valid attribute
  251. //~^^ ERROR cannot find attribute `bar` in this scope
  252. bar: String,
  253. }
  254. #[derive(SessionSubdiagnostic)]
  255. #[label(slug = "label-aa")]
  256. struct AA {
  257. #[primary_span]
  258. span: Span,
  259. #[skip_arg]
  260. z: Z
  261. }
  262. #[derive(SessionSubdiagnostic)]
  263. union AB {
  264. //~^ ERROR unexpected unsupported untagged union
  265. span: u32,
  266. b: u64
  267. }
  268. #[derive(SessionSubdiagnostic)]
  269. #[label(slug = "label-ac-1")]
  270. //~^ NOTE previously specified here
  271. //~^^ NOTE previously specified here
  272. #[label(slug = "label-ac-2")]
  273. //~^ ERROR specified multiple times
  274. //~^^ ERROR specified multiple times
  275. struct AC {
  276. #[primary_span]
  277. span: Span,
  278. }
  279. #[derive(SessionSubdiagnostic)]
  280. #[label(slug = "label-ad-1", slug = "label-ad-2")]
  281. //~^ ERROR specified multiple times
  282. //~^^ NOTE previously specified here
  283. struct AD {
  284. #[primary_span]
  285. span: Span,
  286. }
  287. #[derive(SessionSubdiagnostic)]
  288. #[label(slug = "label-ad-1")]
  289. struct AE {
  290. #[primary_span]
  291. //~^ NOTE previously specified here
  292. span_a: Span,
  293. #[primary_span]
  294. //~^ ERROR specified multiple times
  295. span_b: Span,
  296. }
  297. #[derive(SessionSubdiagnostic)]
  298. struct AF {
  299. //~^ ERROR subdiagnostic kind not specified
  300. #[primary_span]
  301. span: Span,
  302. }
  303. #[derive(SessionSubdiagnostic)]
  304. #[suggestion(slug = "suggestion-af", code = "...")]
  305. struct AG {
  306. #[primary_span]
  307. span: Span,
  308. #[applicability]
  309. applicability: Applicability,
  310. var: String,
  311. }
  312. #[derive(SessionSubdiagnostic)]
  313. enum AH {
  314. #[suggestion(slug = "suggestion-ag-a", code = "...")]
  315. A {
  316. #[primary_span]
  317. span: Span,
  318. #[applicability]
  319. applicability: Applicability,
  320. var: String,
  321. },
  322. #[suggestion(slug = "suggestion-ag-b", code = "...")]
  323. B {
  324. #[primary_span]
  325. span: Span,
  326. #[applicability]
  327. applicability: Applicability,
  328. var: String,
  329. }
  330. }
  331. #[derive(SessionSubdiagnostic)]
  332. #[suggestion(slug = "...", code = "...", code = "...")]
  333. //~^ ERROR specified multiple times
  334. //~^^ NOTE previously specified here
  335. struct AI {
  336. #[primary_span]
  337. span: Span,
  338. #[applicability]
  339. applicability: Applicability,
  340. }
  341. #[derive(SessionSubdiagnostic)]
  342. #[suggestion(slug = "...", code = "...")]
  343. struct AJ {
  344. #[primary_span]
  345. span: Span,
  346. #[applicability]
  347. //~^ NOTE previously specified here
  348. applicability_a: Applicability,
  349. #[applicability]
  350. //~^ ERROR specified multiple times
  351. applicability_b: Applicability,
  352. }
  353. #[derive(SessionSubdiagnostic)]
  354. #[suggestion(slug = "...", code = "...")]
  355. //~^ ERROR suggestion without `applicability`
  356. struct AK {
  357. #[primary_span]
  358. span: Span,
  359. #[applicability]
  360. //~^ ERROR the `#[applicability]` attribute can only be applied to fields of type `Applicability`
  361. applicability: Span,
  362. }
  363. #[derive(SessionSubdiagnostic)]
  364. #[suggestion(slug = "...", code = "...")]
  365. //~^ ERROR suggestion without `applicability`
  366. struct AL {
  367. #[primary_span]
  368. span: Span,
  369. }
  370. #[derive(SessionSubdiagnostic)]
  371. #[suggestion(slug = "...")]
  372. //~^ ERROR suggestion without `code = "..."`
  373. struct AM {
  374. #[primary_span]
  375. span: Span,
  376. #[applicability]
  377. applicability: Applicability,
  378. }
  379. #[derive(SessionSubdiagnostic)]
  380. #[suggestion(slug = "...", code ="...", applicability = "foo")]
  381. //~^ ERROR invalid applicability
  382. struct AN {
  383. #[primary_span]
  384. span: Span,
  385. }
  386. #[derive(SessionSubdiagnostic)]
  387. #[help(slug = "label-am")]
  388. struct AO {
  389. var: String
  390. }
  391. #[derive(SessionSubdiagnostic)]
  392. #[note(slug = "label-an")]
  393. struct AP;
  394. #[derive(SessionSubdiagnostic)]
  395. #[suggestion(slug = "...", code = "...")]
  396. //~^ ERROR suggestion without `applicability`
  397. //~^^ ERROR suggestion without `#[primary_span]` field
  398. struct AQ {
  399. var: String,
  400. }
  401. #[derive(SessionSubdiagnostic)]
  402. #[suggestion(slug = "...", code ="...", applicability = "machine-applicable")]
  403. struct AR {
  404. #[primary_span]
  405. span: Span,
  406. }
  407. #[derive(SessionSubdiagnostic)]
  408. #[label]
  409. //~^ ERROR unsupported type attribute for subdiagnostic enum
  410. enum AS {
  411. #[label(slug = "...")]
  412. A {
  413. #[primary_span]
  414. span: Span,
  415. var: String,
  416. }
  417. }
  418. #[derive(SessionSubdiagnostic)]
  419. #[suggestion(slug = "...", code ="{var}", applicability = "machine-applicable")]
  420. struct AT {
  421. #[primary_span]
  422. span: Span,
  423. var: String,
  424. }
  425. #[derive(SessionSubdiagnostic)]
  426. #[suggestion(slug = "...", code ="{var}", applicability = "machine-applicable")]
  427. //~^ ERROR `var` doesn't refer to a field on this type
  428. struct AU {
  429. #[primary_span]
  430. span: Span,
  431. }
  432. #[derive(SessionSubdiagnostic)]
  433. enum AV {
  434. #[suggestion(slug = "...", code ="{var}", applicability = "machine-applicable")]
  435. A {
  436. #[primary_span]
  437. span: Span,
  438. var: String,
  439. }
  440. }
  441. #[derive(SessionSubdiagnostic)]
  442. enum AW {
  443. #[suggestion(slug = "...", code ="{var}", applicability = "machine-applicable")]
  444. //~^ ERROR `var` doesn't refer to a field on this type
  445. A {
  446. #[primary_span]
  447. span: Span,
  448. }
  449. }