1use rustc_ast::{
2 self as ast, AttrVec, DUMMY_NODE_ID, GenericBounds, GenericParam, GenericParamKind, TyKind,
3 WhereClause, token,
4};
5use rustc_errors::{Applicability, Diag, PResult};
6use rustc_span::{Ident, Span, kw, sym};
7use thin_vec::ThinVec;
8
9use super::{ForceCollect, Parser, Trailing, UsePreAttrPos};
10use crate::diagnostics::{
11 self, MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters,
12 UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
13 WhereClauseBeforeTupleStructBodySugg,
14};
15use crate::exp;
16
17enum PredicateKindOrStructBody {
18 PredicateKind(ast::WherePredicateKind),
19 StructBody(ThinVec<ast::FieldDef>),
20}
21
22impl<'a> Parser<'a> {
23 fn parse_lt_param_bounds(&mut self) -> GenericBounds {
29 let mut lifetimes = ThinVec::new();
30 while self.check_lifetime() {
31 lifetimes.push(ast::GenericBound::Outlives(self.expect_lifetime()));
32
33 if !self.eat_plus() {
34 break;
35 }
36 }
37 lifetimes
38 }
39
40 fn parse_ty_param(&mut self, preceding_attrs: AttrVec) -> PResult<'a, GenericParam> {
42 let ident = self.parse_ident()?;
43
44 if self.may_recover()
46 && ident.name.as_str().to_ascii_lowercase() == kw::Const.as_str()
47 && self.check_ident()
48 {
50 return self.recover_const_param_with_mistyped_const(preceding_attrs, ident);
51 }
52
53 let mut colon_span = None;
55 let bounds = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
56 colon_span = Some(self.prev_token.span);
57 if self.token.is_keyword(kw::Impl) {
59 let impl_span = self.token.span;
60 let snapshot = self.create_snapshot_for_diagnostic();
61 match self.parse_ty() {
62 Ok(p) => {
63 if let TyKind::ImplTrait(_, bounds) = &p.kind {
64 let span = impl_span.to(self.token.span.shrink_to_lo());
65 let mut err = self.dcx().struct_span_err(
66 span,
67 "expected trait bound, found `impl Trait` type",
68 );
69 err.span_label(span, "not a trait");
70 if let [bound, ..] = &bounds[..] {
71 err.span_suggestion_verbose(
72 impl_span.until(bound.span()),
73 "use the trait bounds directly",
74 String::new(),
75 Applicability::MachineApplicable,
76 );
77 }
78 return Err(err);
79 }
80 }
81 Err(err) => {
82 err.cancel();
83 }
84 }
85 self.restore_snapshot(snapshot);
86 }
87 self.parse_generic_bounds()?
88 } else {
89 ThinVec::new()
90 };
91
92 let default = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_ty()?) } else { None };
93 Ok(GenericParam {
94 ident,
95 id: ast::DUMMY_NODE_ID,
96 attrs: preceding_attrs,
97 bounds,
98 kind: GenericParamKind::Type { default },
99 is_placeholder: false,
100 colon_span,
101 })
102 }
103
104 pub(crate) fn parse_const_param(
105 &mut self,
106 preceding_attrs: AttrVec,
107 ) -> PResult<'a, GenericParam> {
108 let const_span = self.token.span;
109
110 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
111 let ident = self.parse_ident()?;
112 if let Err(mut err) = self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
113 return if self.token.kind == token::Comma || self.token.kind == token::Gt {
114 let span = const_span.to(ident.span);
116 err.span_suggestion_verbose(
117 ident.span.shrink_to_hi(),
118 "you likely meant to write the type of the const parameter here",
119 ": /* Type */".to_string(),
120 Applicability::HasPlaceholders,
121 );
122 let kind = TyKind::Err(err.emit());
123 let ty = self.mk_ty(span, kind);
124 Ok(GenericParam {
125 ident,
126 id: ast::DUMMY_NODE_ID,
127 attrs: preceding_attrs,
128 bounds: ThinVec::new(),
129 kind: GenericParamKind::Const { ty, span, default: None },
130 is_placeholder: false,
131 colon_span: None,
132 })
133 } else {
134 Err(err)
135 };
136 }
137 let ty = self.parse_ty()?;
138
139 let default = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_const_arg()?) } else { None };
141 let span = if let Some(ref default) = default {
142 const_span.to(default.value.span)
143 } else {
144 const_span.to(ty.span)
145 };
146
147 Ok(GenericParam {
148 ident,
149 id: ast::DUMMY_NODE_ID,
150 attrs: preceding_attrs,
151 bounds: ThinVec::new(),
152 kind: GenericParamKind::Const { ty, span, default },
153 is_placeholder: false,
154 colon_span: None,
155 })
156 }
157
158 pub(crate) fn recover_const_param_with_mistyped_const(
159 &mut self,
160 preceding_attrs: AttrVec,
161 mistyped_const_ident: Ident,
162 ) -> PResult<'a, GenericParam> {
163 let ident = self.parse_ident()?;
164 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon))?;
165 let ty = self.parse_ty()?;
166
167 let default = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_const_arg()?) } else { None };
169 let span = if let Some(ref default) = default {
170 mistyped_const_ident.span.to(default.value.span)
171 } else {
172 mistyped_const_ident.span.to(ty.span)
173 };
174
175 self.dcx()
176 .struct_span_err(
177 mistyped_const_ident.span,
178 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`const` keyword was mistyped as `{0}`",
mistyped_const_ident.as_str()))
})format!("`const` keyword was mistyped as `{}`", mistyped_const_ident.as_str()),
179 )
180 .with_span_suggestion_verbose(
181 mistyped_const_ident.span,
182 "use the `const` keyword",
183 kw::Const,
184 Applicability::MachineApplicable,
185 )
186 .emit();
187
188 Ok(GenericParam {
189 ident,
190 id: ast::DUMMY_NODE_ID,
191 attrs: preceding_attrs,
192 bounds: ThinVec::new(),
193 kind: GenericParamKind::Const { ty, span, default },
194 is_placeholder: false,
195 colon_span: None,
196 })
197 }
198
199 pub(super) fn parse_generic_params(&mut self) -> PResult<'a, ThinVec<ast::GenericParam>> {
205 let mut params = ThinVec::new();
206 let mut done = false;
207 let prev = self.parsing_generics;
208 self.parsing_generics = true;
209 while !done {
210 let attrs = self.parse_outer_attributes()?;
211 let param = match self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
212 if this.eat_keyword_noexpect(kw::SelfUpper) {
213 this.dcx()
216 .emit_err(UnexpectedSelfInGenericParameters { span: this.prev_token.span });
217
218 let _ = this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
220 }
221
222 let param = if this.check_lifetime() {
223 let lifetime = this.expect_lifetime();
224 let (colon_span, bounds) = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
226 (Some(this.prev_token.span), this.parse_lt_param_bounds())
227 } else {
228 (None, ThinVec::new())
229 };
230
231 if this.check_noexpect(&token::Eq) && this.look_ahead(1, |t| t.is_lifetime()) {
232 let lo = this.token.span;
233 this.bump(); this.bump(); let span = lo.to(this.prev_token.span);
237 this.dcx().emit_err(UnexpectedDefaultValueForLifetimeInGenericParameters {
238 span,
239 });
240 }
241
242 Some(ast::GenericParam {
243 ident: lifetime.ident,
244 id: lifetime.id,
245 attrs,
246 bounds,
247 kind: ast::GenericParamKind::Lifetime,
248 is_placeholder: false,
249 colon_span,
250 })
251 } else if this.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
252 Some(this.parse_const_param(attrs)?)
254 } else if this.check_ident() {
255 Some(this.parse_ty_param(attrs)?)
257 } else if this.token.can_begin_type() {
258 let snapshot = this.create_snapshot_for_diagnostic();
260 let lo = this.token.span;
261 match this.parse_ty_where_predicate_kind() {
262 Ok(_) => {
263 this.dcx().emit_err(diagnostics::BadAssocTypeBounds {
264 span: lo.to(this.prev_token.span),
265 });
266 }
268 Err(err) => {
269 err.cancel();
270 this.restore_snapshot(snapshot);
272 }
273 }
274 return Ok((None, Trailing::No, UsePreAttrPos::No));
275 } else {
276 if !attrs.is_empty() {
278 if !params.is_empty() {
279 this.dcx()
280 .emit_err(diagnostics::AttrAfterGeneric { span: attrs[0].span });
281 } else {
282 this.dcx()
283 .emit_err(diagnostics::AttrWithoutGenerics { span: attrs[0].span });
284 }
285 }
286 return Ok((None, Trailing::No, UsePreAttrPos::No));
287 };
288
289 if !this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
290 done = true;
291 }
292 Ok((param, Trailing::No, UsePreAttrPos::No))
294 }) {
295 Ok(param) => param,
296 Err(err) => {
297 self.parsing_generics = prev;
298 return Err(err);
299 }
300 };
301
302 if let Some(param) = param {
303 params.push(param);
304 } else {
305 break;
306 }
307 }
308 self.parsing_generics = prev;
309 Ok(params)
310 }
311
312 pub(super) fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
320 if self.eat_noexpect(&token::PathSep) {
323 self.dcx()
324 .emit_err(diagnostics::InvalidPathSepInFnDefinition { span: self.prev_token.span });
325 }
326
327 let span_lo = self.token.span;
328 let (params, span) = if self.eat_lt() {
329 let params = self.parse_generic_params()?;
330 self.expect_gt_or_maybe_suggest_closing_generics(¶ms)?;
331 (params, span_lo.to(self.prev_token.span))
332 } else {
333 (ThinVec::new(), self.prev_token.span.shrink_to_hi())
334 };
335 Ok(ast::Generics {
336 params,
337 where_clause: WhereClause {
338 has_where_token: false,
339 predicates: ThinVec::new(),
340 span: self.prev_token.span.shrink_to_hi(),
341 },
342 span,
343 })
344 }
345
346 pub(super) fn parse_contract(&mut self) -> PResult<'a, Option<Box<ast::FnContract>>> {
349 let (declarations, requires) = self.parse_contract_requires()?;
350 let ensures = self.parse_contract_ensures()?;
351
352 if requires.is_none() && ensures.is_none() {
353 Ok(None)
354 } else {
355 Ok(Some(Box::new(ast::FnContract { declarations, requires, ensures })))
356 }
357 }
358
359 fn parse_contract_requires(
360 &mut self,
361 ) -> PResult<'a, (ThinVec<rustc_ast::Stmt>, Option<Box<rustc_ast::Expr>>)> {
362 Ok(if self.eat_keyword_noexpect(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::ContractRequires,
token_type: crate::parser::token_type::TokenType::KwContractRequires,
}exp!(ContractRequires).kw) {
363 self.psess.gated_spans.gate(sym::contracts_internals, self.prev_token.span);
364 let mut decls_and_precond = self.parse_block()?;
365
366 let precond = match decls_and_precond.stmts.pop() {
367 Some(precond) => match precond.kind {
368 rustc_ast::StmtKind::Expr(expr) => expr,
369 _ => self.mk_unit_expr(decls_and_precond.span),
372 },
373 None => self.mk_unit_expr(decls_and_precond.span),
374 };
375 let precond = self.mk_closure_expr(precond.span, precond);
376 let decls = decls_and_precond.stmts;
377 (decls, Some(precond))
378 } else {
379 (Default::default(), None)
380 })
381 }
382
383 fn parse_contract_ensures(&mut self) -> PResult<'a, Option<Box<rustc_ast::Expr>>> {
384 Ok(if self.eat_keyword_noexpect(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::ContractEnsures,
token_type: crate::parser::token_type::TokenType::KwContractEnsures,
}exp!(ContractEnsures).kw) {
385 self.psess.gated_spans.gate(sym::contracts_internals, self.prev_token.span);
386 let postcond = self.parse_expr()?;
387 Some(postcond)
388 } else {
389 None
390 })
391 }
392
393 pub(super) fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
399 self.parse_where_clause_common(None).map(|(clause, _)| clause)
400 }
401
402 pub(super) fn parse_struct_where_clause(
403 &mut self,
404 struct_name: Ident,
405 body_insertion_point: Span,
406 ) -> PResult<'a, (WhereClause, Option<ThinVec<ast::FieldDef>>)> {
407 self.parse_where_clause_common(Some((struct_name, body_insertion_point)))
408 }
409
410 fn parse_where_clause_common(
411 &mut self,
412 struct_: Option<(Ident, Span)>,
413 ) -> PResult<'a, (WhereClause, Option<ThinVec<ast::FieldDef>>)> {
414 let mut where_clause = WhereClause {
415 has_where_token: false,
416 predicates: ThinVec::new(),
417 span: self.prev_token.span.shrink_to_hi(),
418 };
419 let mut tuple_struct_body = None;
420
421 if !self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Where,
token_type: crate::parser::token_type::TokenType::KwWhere,
}exp!(Where)) {
422 return Ok((where_clause, None));
423 }
424
425 if self.eat_noexpect(&token::Colon) {
426 let colon_span = self.prev_token.span;
427 self.dcx()
428 .struct_span_err(colon_span, "unexpected colon after `where`")
429 .with_span_suggestion_short(
430 colon_span,
431 "remove the colon",
432 "",
433 Applicability::MachineApplicable,
434 )
435 .emit();
436 }
437
438 where_clause.has_where_token = true;
439 let where_lo = self.prev_token.span;
440
441 if self.choose_generics_over_qpath(0) {
445 let generics = self.parse_generics()?;
446 self.dcx().emit_err(diagnostics::WhereOnGenerics { span: generics.span });
447 }
448
449 loop {
450 let where_sp = where_lo.to(self.prev_token.span);
451 let attrs = self.parse_outer_attributes()?;
452 let pred_lo = self.token.span;
453 let predicate = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
454 for attr in &attrs {
455 self.psess.gated_spans.gate(sym::where_clause_attrs, attr.span);
456 }
457 let kind = if this.check_lifetime() && this.look_ahead(1, |t| !t.is_like_plus()) {
458 let lifetime = this.expect_lifetime();
459 this.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon))?;
461 let bounds = this.parse_lt_param_bounds();
462 Some(ast::WherePredicateKind::RegionPredicate(ast::WhereRegionPredicate {
463 lifetime,
464 bounds,
465 }))
466 } else if this.check_type() {
467 match this.parse_ty_where_predicate_kind_or_recover_tuple_struct_body(
468 struct_, pred_lo, where_sp,
469 )? {
470 PredicateKindOrStructBody::PredicateKind(kind) => Some(kind),
471 PredicateKindOrStructBody::StructBody(body) => {
472 tuple_struct_body = Some(body);
473 None
474 }
475 }
476 } else {
477 if let [.., last] = &attrs[..] {
478 if last.is_doc_comment() {
479 this.dcx().emit_err(diagnostics::DocCommentDoesNotDocumentAnything {
480 span: last.span,
481 missing_comma: None,
482 });
483 } else {
484 this.dcx().emit_err(diagnostics::AttrWithoutWherePredicates {
485 span: last.span,
486 });
487 }
488 }
489 None
490 };
491 let predicate = kind.map(|kind| ast::WherePredicate {
492 attrs,
493 kind,
494 id: DUMMY_NODE_ID,
495 span: pred_lo.to(this.prev_token.span),
496 is_placeholder: false,
497 });
498 Ok((predicate, Trailing::No, UsePreAttrPos::No))
499 })?;
500 match predicate {
501 Some(predicate) => where_clause.predicates.push(predicate),
502 None => break,
503 }
504
505 let prev_token = self.prev_token.span;
506 let ate_comma = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
507
508 if self.eat_keyword_noexpect(kw::Where) {
509 self.dcx().emit_err(MultipleWhereClauses {
510 span: self.token.span,
511 previous: pred_lo,
512 between: prev_token.shrink_to_hi().to(self.prev_token.span),
513 });
514 } else if !ate_comma {
515 break;
516 }
517 }
518
519 where_clause.span = where_lo.to(self.prev_token.span);
520 Ok((where_clause, tuple_struct_body))
521 }
522
523 fn parse_ty_where_predicate_kind_or_recover_tuple_struct_body(
524 &mut self,
525 struct_: Option<(Ident, Span)>,
526 pred_lo: Span,
527 where_sp: Span,
528 ) -> PResult<'a, PredicateKindOrStructBody> {
529 let mut snapshot = None;
530
531 if let Some(struct_) = struct_
532 && self.may_recover()
533 && self.token == token::OpenParen
534 {
535 snapshot = Some((struct_, self.create_snapshot_for_diagnostic()));
536 };
537
538 match self.parse_ty_where_predicate_kind() {
539 Ok(pred) => Ok(PredicateKindOrStructBody::PredicateKind(pred)),
540 Err(type_err) => {
541 let Some(((struct_name, body_insertion_point), mut snapshot)) = snapshot else {
542 return Err(type_err);
543 };
544
545 match snapshot.parse_tuple_struct_body() {
547 Ok(body)
553 if #[allow(non_exhaustive_omitted_patterns)] match snapshot.token.kind {
token::Semi | token::Eof => true,
_ => false,
}matches!(snapshot.token.kind, token::Semi | token::Eof)
554 || snapshot.token.can_begin_item() =>
555 {
556 type_err.cancel();
557
558 let body_sp = pred_lo.to(snapshot.prev_token.span);
559 let map = self.psess.source_map();
560
561 self.dcx().emit_err(WhereClauseBeforeTupleStructBody {
562 span: where_sp,
563 name: struct_name.span,
564 body: body_sp,
565 sugg: map.span_to_snippet(body_sp).ok().map(|body| {
566 WhereClauseBeforeTupleStructBodySugg {
567 left: body_insertion_point.shrink_to_hi(),
568 snippet: body,
569 right: map.end_point(where_sp).to(body_sp),
570 }
571 }),
572 });
573
574 self.restore_snapshot(snapshot);
575 Ok(PredicateKindOrStructBody::StructBody(body))
576 }
577 Ok(_) => Err(type_err),
578 Err(body_err) => {
579 body_err.cancel();
580 Err(type_err)
581 }
582 }
583 }
584 }
585 }
586
587 fn parse_ty_where_predicate_kind(&mut self) -> PResult<'a, ast::WherePredicateKind> {
588 let (bound_vars, _) = self.parse_higher_ranked_binder()?;
596
597 let ty = self.parse_ty_for_where_clause()?;
598
599 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
600 let bounds = self.parse_generic_bounds()?;
602
603 return Ok(ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
604 bound_generic_params: bound_vars,
605 bounded_ty: ty,
606 bounds,
607 }));
608 }
609
610 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) || self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::EqEq,
token_type: crate::parser::token_type::TokenType::EqEq,
}exp!(EqEq)) {
613 let lhs_ty = ty;
614 let rhs_ty = self.parse_ty()?;
615
616 let _ = bound_vars;
619
620 let mut diag = self.dcx().struct_span_err(
621 lhs_ty.span.to(rhs_ty.span),
622 "general type equality constraints are not supported",
623 );
624 diag.note(
625 "see issue #20041 <https://github.com/rust-lang/rust/issues/20041> \
626 for more information",
627 );
628 diag.span(lhs_ty.span.to(rhs_ty.span));
629 diag.span_label(lhs_ty.span.to(rhs_ty.span), "not supported");
630
631 suggest_replacing_equality_pred_with_assoc_item_constraint(&mut diag, *lhs_ty, *rhs_ty);
632
633 return Err(diag);
634 }
635
636 self.maybe_recover_bounds_doubled_colon(&ty)?;
637 self.unexpected_any()
638 }
639
640 pub(super) fn choose_generics_over_qpath(&self, start: usize) -> bool {
641 self.look_ahead(start, |t| t == &token::Lt)
660 && (self.look_ahead(start + 1, |t| t == &token::Pound || t == &token::Gt)
661 || self.look_ahead(start + 1, |t| t.is_lifetime() || t.is_ident())
662 && self.look_ahead(start + 2, |t| {
663 #[allow(non_exhaustive_omitted_patterns)] match t.kind {
token::Gt | token::Comma | token::Colon | token::Eq => true,
_ => false,
}matches!(t.kind, token::Gt | token::Comma | token::Colon | token::Eq)
664 || t.kind == token::Question
667 })
668 || self.is_keyword_ahead(start + 1, &[kw::Const]))
669 }
670}
671
672fn suggest_replacing_equality_pred_with_assoc_item_constraint(
673 diag: &mut Diag<'_>,
674 lhs_ty: ast::Ty,
675 rhs_ty: ast::Ty,
676) {
677 let TyKind::Path(qself, ast::Path { segments, .. }) = lhs_ty.kind else { return };
678
679 let mut parts = Vec::new();
680 let applicability = match qself {
681 None if let [self_ty_seg, assoc_item_seg] = &segments[..]
683 && self_ty_seg.ident.name != kw::PathRoot =>
684 {
685 parts.push((
686 self_ty_seg.span().between(assoc_item_seg.span()),
687 ": /* Trait */</* ... */".into(),
688 ));
689 Applicability::HasPlaceholders
690 }
691 Some(qself) if let [assoc_item_seg] = &segments[qself.position..] => {
692 parts.push((lhs_ty.span.until(qself.ty.span), String::new()));
693
694 if let trait_segs @ [.., final_trait_seg] = &segments[..qself.position] {
696 parts.push((qself.ty.span.between(trait_segs[0].span()), ": ".into()));
697 let (span, snippet) = match &final_trait_seg.args {
698 Some(args) => {
699 let ast::GenericArgs::AngleBracketed(args) = args else { return };
700 let Some(args) = args.args.last() else { return };
701 (args.span(), ", ")
702 }
703 None => (final_trait_seg.span(), "<"),
704 };
705 parts.push((span.between(assoc_item_seg.span()), snippet.into()));
706 Applicability::MaybeIncorrect
707 }
708 else {
710 parts.push((
711 qself.ty.span.between(assoc_item_seg.span()),
712 ": /* Trait */</* ... */".into(),
713 ));
714 Applicability::HasPlaceholders
715 }
716 }
717 _ => return,
718 };
719
720 parts.push((lhs_ty.span.between(rhs_ty.span), " = ".into()));
721 parts.push((rhs_ty.span.shrink_to_hi(), ">".into()));
722
723 diag.multipart_suggestion(
724 "replace it with an associated item constraint if possible",
725 parts,
726 applicability,
727 );
728}