Skip to content

Commit 716c563

Browse files
compiler-errorscuviper
authored andcommitted
Fix ICEs in diagnostic::on_unimplemented
(cherry picked from commit 7dbdbaa)
1 parent 5d77c79 commit 716c563

File tree

2 files changed

+203
-51
lines changed

2 files changed

+203
-51
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,14 @@ impl IgnoredDiagnosticOption {
349349
option_name: &'static str,
350350
) {
351351
if let (Some(new_item), Some(old_item)) = (new, old) {
352-
tcx.emit_node_span_lint(
353-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
354-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
355-
new_item,
356-
IgnoredDiagnosticOption { span: new_item, prev_span: old_item, option_name },
357-
);
352+
if let Some(item_def_id) = item_def_id.as_local() {
353+
tcx.emit_node_span_lint(
354+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
355+
tcx.local_def_id_to_hir_id(item_def_id),
356+
new_item,
357+
IgnoredDiagnosticOption { span: new_item, prev_span: old_item, option_name },
358+
);
359+
}
358360
}
359361
}
360362
}
@@ -638,30 +640,38 @@ impl<'tcx> OnUnimplementedDirective {
638640
AttrArgs::Eq(span, AttrArgsEq::Hir(expr)) => span.to(expr.span),
639641
};
640642

641-
tcx.emit_node_span_lint(
642-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
643-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
644-
report_span,
645-
MalformedOnUnimplementedAttrLint::new(report_span),
646-
);
643+
if let Some(item_def_id) = item_def_id.as_local() {
644+
tcx.emit_node_span_lint(
645+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
646+
tcx.local_def_id_to_hir_id(item_def_id),
647+
report_span,
648+
MalformedOnUnimplementedAttrLint::new(report_span),
649+
);
650+
}
647651
Ok(None)
648652
}
649653
} else if is_diagnostic_namespace_variant {
650654
match &attr.kind {
651655
AttrKind::Normal(p) if !matches!(p.item.args, AttrArgs::Empty) => {
652-
tcx.emit_node_span_lint(
653-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
654-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
655-
attr.span,
656-
MalformedOnUnimplementedAttrLint::new(attr.span),
657-
);
656+
if let Some(item_def_id) = item_def_id.as_local() {
657+
tcx.emit_node_span_lint(
658+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
659+
tcx.local_def_id_to_hir_id(item_def_id),
660+
attr.span,
661+
MalformedOnUnimplementedAttrLint::new(attr.span),
662+
);
663+
}
664+
}
665+
_ => {
666+
if let Some(item_def_id) = item_def_id.as_local() {
667+
tcx.emit_node_span_lint(
668+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
669+
tcx.local_def_id_to_hir_id(item_def_id),
670+
attr.span,
671+
MissingOptionsForOnUnimplementedAttr,
672+
)
673+
}
658674
}
659-
_ => tcx.emit_node_span_lint(
660-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
661-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
662-
attr.span,
663-
MissingOptionsForOnUnimplementedAttr,
664-
),
665675
};
666676

667677
Ok(None)
@@ -790,12 +800,14 @@ impl<'tcx> OnUnimplementedFormatString {
790800
|| format_spec.precision_span.is_some()
791801
|| format_spec.fill_span.is_some())
792802
{
793-
tcx.emit_node_span_lint(
794-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
795-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
796-
self.span,
797-
InvalidFormatSpecifier,
798-
);
803+
if let Some(item_def_id) = item_def_id.as_local() {
804+
tcx.emit_node_span_lint(
805+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
806+
tcx.local_def_id_to_hir_id(item_def_id),
807+
self.span,
808+
InvalidFormatSpecifier,
809+
);
810+
}
799811
}
800812
match a.position {
801813
Position::ArgumentNamed(s) => {
@@ -811,15 +823,17 @@ impl<'tcx> OnUnimplementedFormatString {
811823
s if generics.params.iter().any(|param| param.name == s) => (),
812824
s => {
813825
if self.is_diagnostic_namespace_variant {
814-
tcx.emit_node_span_lint(
815-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
816-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
817-
self.span,
818-
UnknownFormatParameterForOnUnimplementedAttr {
819-
argument_name: s,
820-
trait_name,
821-
},
822-
);
826+
if let Some(item_def_id) = item_def_id.as_local() {
827+
tcx.emit_node_span_lint(
828+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
829+
tcx.local_def_id_to_hir_id(item_def_id),
830+
self.span,
831+
UnknownFormatParameterForOnUnimplementedAttr {
832+
argument_name: s,
833+
trait_name,
834+
},
835+
);
836+
}
823837
} else {
824838
result = Err(struct_span_code_err!(
825839
tcx.dcx(),
@@ -841,12 +855,14 @@ impl<'tcx> OnUnimplementedFormatString {
841855
// `{:1}` and `{}` are not to be used
842856
Position::ArgumentIs(..) | Position::ArgumentImplicitlyIs(_) => {
843857
if self.is_diagnostic_namespace_variant {
844-
tcx.emit_node_span_lint(
845-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
846-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
847-
self.span,
848-
DisallowedPositionalArgument,
849-
);
858+
if let Some(item_def_id) = item_def_id.as_local() {
859+
tcx.emit_node_span_lint(
860+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
861+
tcx.local_def_id_to_hir_id(item_def_id),
862+
self.span,
863+
DisallowedPositionalArgument,
864+
);
865+
}
850866
} else {
851867
let reported = struct_span_code_err!(
852868
tcx.dcx(),
@@ -869,12 +885,14 @@ impl<'tcx> OnUnimplementedFormatString {
869885
// so that users are aware that something is not correct
870886
for e in parser.errors {
871887
if self.is_diagnostic_namespace_variant {
872-
tcx.emit_node_span_lint(
873-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
874-
tcx.local_def_id_to_hir_id(item_def_id.expect_local()),
875-
self.span,
876-
WrappedParserError { description: e.description, label: e.label },
877-
);
888+
if let Some(item_def_id) = item_def_id.as_local() {
889+
tcx.emit_node_span_lint(
890+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
891+
tcx.local_def_id_to_hir_id(item_def_id),
892+
self.span,
893+
WrappedParserError { description: e.description, label: e.label },
894+
);
895+
}
878896
} else {
879897
let reported =
880898
struct_span_code_err!(tcx.dcx(), self.span, E0231, "{}", e.description,).emit();
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
error[E0277]: the trait bound `(): bad_on_unimplemented::MissingAttr` is not satisfied
2+
--> $DIR/malformed_foreign_on_unimplemented.rs:22:18
3+
|
4+
LL | missing_attr(());
5+
| ------------ ^^ the trait `bad_on_unimplemented::MissingAttr` is not implemented for `()`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
note: required by a bound in `missing_attr`
10+
--> $DIR/malformed_foreign_on_unimplemented.rs:11:20
11+
|
12+
LL | fn missing_attr<T: MissingAttr>(_: T) {}
13+
| ^^^^^^^^^^^ required by this bound in `missing_attr`
14+
15+
error[E0277]: the trait bound `(): bad_on_unimplemented::DuplicateAttr` is not satisfied
16+
--> $DIR/malformed_foreign_on_unimplemented.rs:23:20
17+
|
18+
LL | duplicate_attr(());
19+
| -------------- ^^ a
20+
| |
21+
| required by a bound introduced by this call
22+
|
23+
= help: the trait `bad_on_unimplemented::DuplicateAttr` is not implemented for `()`
24+
note: required by a bound in `duplicate_attr`
25+
--> $DIR/malformed_foreign_on_unimplemented.rs:12:22
26+
|
27+
LL | fn duplicate_attr<T: DuplicateAttr>(_: T) {}
28+
| ^^^^^^^^^^^^^ required by this bound in `duplicate_attr`
29+
30+
error[E0277]: the trait bound `(): bad_on_unimplemented::NotMetaList` is not satisfied
31+
--> $DIR/malformed_foreign_on_unimplemented.rs:24:19
32+
|
33+
LL | not_meta_list(());
34+
| ------------- ^^ the trait `bad_on_unimplemented::NotMetaList` is not implemented for `()`
35+
| |
36+
| required by a bound introduced by this call
37+
|
38+
note: required by a bound in `not_meta_list`
39+
--> $DIR/malformed_foreign_on_unimplemented.rs:13:21
40+
|
41+
LL | fn not_meta_list<T: NotMetaList>(_: T) {}
42+
| ^^^^^^^^^^^ required by this bound in `not_meta_list`
43+
44+
error[E0277]: the trait bound `(): bad_on_unimplemented::Empty` is not satisfied
45+
--> $DIR/malformed_foreign_on_unimplemented.rs:25:11
46+
|
47+
LL | empty(());
48+
| ----- ^^ the trait `bad_on_unimplemented::Empty` is not implemented for `()`
49+
| |
50+
| required by a bound introduced by this call
51+
|
52+
note: required by a bound in `empty`
53+
--> $DIR/malformed_foreign_on_unimplemented.rs:14:13
54+
|
55+
LL | fn empty<T: Empty>(_: T) {}
56+
| ^^^^^ required by this bound in `empty`
57+
58+
error[E0277]: the trait bound `(): bad_on_unimplemented::WrongDelim` is not satisfied
59+
--> $DIR/malformed_foreign_on_unimplemented.rs:26:17
60+
|
61+
LL | wrong_delim(());
62+
| ----------- ^^ the trait `bad_on_unimplemented::WrongDelim` is not implemented for `()`
63+
| |
64+
| required by a bound introduced by this call
65+
|
66+
note: required by a bound in `wrong_delim`
67+
--> $DIR/malformed_foreign_on_unimplemented.rs:15:19
68+
|
69+
LL | fn wrong_delim<T: WrongDelim>(_: T) {}
70+
| ^^^^^^^^^^ required by this bound in `wrong_delim`
71+
72+
error[E0277]: the trait bound `(): bad_on_unimplemented::BadFormatter<()>` is not satisfied
73+
--> $DIR/malformed_foreign_on_unimplemented.rs:27:19
74+
|
75+
LL | bad_formatter(());
76+
| ------------- ^^ ()
77+
| |
78+
| required by a bound introduced by this call
79+
|
80+
= help: the trait `bad_on_unimplemented::BadFormatter<()>` is not implemented for `()`
81+
note: required by a bound in `bad_formatter`
82+
--> $DIR/malformed_foreign_on_unimplemented.rs:16:21
83+
|
84+
LL | fn bad_formatter<T: BadFormatter<()>>(_: T) {}
85+
| ^^^^^^^^^^^^^^^^ required by this bound in `bad_formatter`
86+
87+
error[E0277]: the trait bound `(): bad_on_unimplemented::NoImplicitArgs` is not satisfied
88+
--> $DIR/malformed_foreign_on_unimplemented.rs:28:22
89+
|
90+
LL | no_implicit_args(());
91+
| ---------------- ^^ test {}
92+
| |
93+
| required by a bound introduced by this call
94+
|
95+
= help: the trait `bad_on_unimplemented::NoImplicitArgs` is not implemented for `()`
96+
note: required by a bound in `no_implicit_args`
97+
--> $DIR/malformed_foreign_on_unimplemented.rs:17:24
98+
|
99+
LL | fn no_implicit_args<T: NoImplicitArgs>(_: T) {}
100+
| ^^^^^^^^^^^^^^ required by this bound in `no_implicit_args`
101+
102+
error[E0277]: the trait bound `(): bad_on_unimplemented::MissingArg` is not satisfied
103+
--> $DIR/malformed_foreign_on_unimplemented.rs:29:17
104+
|
105+
LL | missing_arg(());
106+
| ----------- ^^ {missing}
107+
| |
108+
| required by a bound introduced by this call
109+
|
110+
= help: the trait `bad_on_unimplemented::MissingArg` is not implemented for `()`
111+
note: required by a bound in `missing_arg`
112+
--> $DIR/malformed_foreign_on_unimplemented.rs:18:19
113+
|
114+
LL | fn missing_arg<T: MissingArg>(_: T) {}
115+
| ^^^^^^^^^^ required by this bound in `missing_arg`
116+
117+
error[E0277]: the trait bound `(): bad_on_unimplemented::BadArg` is not satisfied
118+
--> $DIR/malformed_foreign_on_unimplemented.rs:30:13
119+
|
120+
LL | bad_arg(());
121+
| ------- ^^ {_}
122+
| |
123+
| required by a bound introduced by this call
124+
|
125+
= help: the trait `bad_on_unimplemented::BadArg` is not implemented for `()`
126+
note: required by a bound in `bad_arg`
127+
--> $DIR/malformed_foreign_on_unimplemented.rs:19:15
128+
|
129+
LL | fn bad_arg<T: BadArg>(_: T) {}
130+
| ^^^^^^ required by this bound in `bad_arg`
131+
132+
error: aborting due to 9 previous errors
133+
134+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)