Skip to content

Commit 5cc488d

Browse files
committed
deduplicate projection error (E0271) messages
The `ErrorId` variant takes a u16 so that `DiagnosticMessageId` can retain its `Copy` status (the present author's first choice having been the "EXXX" code as a string). The duplicated "type mismatch resolving `{}`" literal is unfortunate, but the `struct_span_err!` macro (which we want to mark that error code as used) is fussy about taking a literal, and the one-time-diagnostics set needs an owned string. This is concerning #33941 and probably #45805!
1 parent 7f43981 commit 5cc488d

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

src/librustc/session/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,13 @@ enum DiagnosticBuilderMethod {
164164
// add more variants as needed to support one-time diagnostics
165165
}
166166

167-
/// Diagnostic message id - used in order to avoid emitting the same message more than once
167+
/// Diagnostic message ID—used by `Session.one_time_diagnostics` to avoid
168+
/// emitting the same message more than once
168169
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
169170
pub enum DiagnosticMessageId {
171+
ErrorId(u16), // EXXXX error code as integer
170172
LintId(lint::LintId),
171-
StabilityId(u32)
173+
StabilityId(u32) // issue number
172174
}
173175

174176
impl Session {

src/librustc/traits/error_reporting.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use middle::const_val;
3636
use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
3737
use std::fmt;
3838
use syntax::ast;
39+
use session::DiagnosticMessageId;
3940
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
4041
use ty::error::ExpectedFound;
4142
use ty::fast_reject;
@@ -219,13 +220,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
219220
}
220221
}
221222

222-
let mut diag = struct_span_err!(
223-
self.tcx.sess, obligation.cause.span, E0271,
224-
"type mismatch resolving `{}`", predicate
225-
);
226-
self.note_type_err(&mut diag, &obligation.cause, None, values, err);
227-
self.note_obligation_cause(&mut diag, obligation);
228-
diag.emit();
223+
let msg = format!("type mismatch resolving `{}`", predicate);
224+
let error_id = (DiagnosticMessageId::ErrorId(271),
225+
Some(obligation.cause.span), msg.clone());
226+
let fresh = self.tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
227+
if fresh {
228+
let mut diag = struct_span_err!(
229+
self.tcx.sess, obligation.cause.span, E0271,
230+
"type mismatch resolving `{}`", predicate
231+
);
232+
self.note_type_err(&mut diag, &obligation.cause, None, values, err);
233+
self.note_obligation_cause(&mut diag, obligation);
234+
diag.emit();
235+
}
229236
});
230237
}
231238

src/test/ui/issue-33941.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::collections::HashMap;
12+
13+
fn main() {
14+
for _ in HashMap::new().iter().cloned() {}
15+
}

src/test/ui/issue-33941.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`
2+
--> $DIR/issue-33941.rs:14:36
3+
|
4+
14 | for _ in HashMap::new().iter().cloned() {}
5+
| ^^^^^^ expected tuple, found reference
6+
|
7+
= note: expected type `(&_, &_)`
8+
found type `&_`
9+
10+
error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _, _> as std::iter::Iterator>::Item == &_`
11+
--> $DIR/issue-33941.rs:14:5
12+
|
13+
14 | for _ in HashMap::new().iter().cloned() {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found reference
15+
|
16+
= note: expected type `(&_, &_)`
17+
found type `&_`
18+
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Cloned<std::collections::hash_map::Iter<'_, _, _>>`
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)