Skip to content

Commit 40cd031

Browse files
committed
Auto merge of rust-lang#109714 - matthiaskrgr:rollup-wipns5h, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#109149 (Improve error message when writer is forgotten in write and writeln macro) - rust-lang#109367 (Streamline fast rejection) - rust-lang#109548 (AnnotationColumn struct to fix hard tab column numbers in errors) - rust-lang#109694 (do not panic on failure to acquire jobserver token) - rust-lang#109705 (new solver: check for intercrate mode when accessing the cache) - rust-lang#109708 (Specialization involving RPITITs is broken so ignore the diagnostic differences) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents acd27bb + 439c68c commit 40cd031

23 files changed

+345
-102
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1452,8 +1452,8 @@ fn start_executing_work<B: ExtraBackendMethods>(
14521452
Err(e) => {
14531453
let msg = &format!("failed to acquire jobserver token: {}", e);
14541454
shared_emitter.fatal(msg);
1455-
// Exit the coordinator thread
1456-
panic!("{}", msg)
1455+
codegen_done = true;
1456+
codegen_aborted = true;
14571457
}
14581458
}
14591459
}

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ impl AnnotateSnippetEmitterWriter {
202202
annotations: annotations
203203
.iter()
204204
.map(|annotation| SourceAnnotation {
205-
range: (annotation.start_col, annotation.end_col),
205+
range: (
206+
annotation.start_col.display,
207+
annotation.end_col.display,
208+
),
206209
label: annotation.label.as_deref().unwrap_or_default(),
207210
annotation_type: annotation_type_for_level(*level),
208211
})

compiler/rustc_errors/src/emitter.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use Destination::*;
1212
use rustc_span::source_map::SourceMap;
1313
use rustc_span::{FileLines, SourceFile, Span};
1414

15-
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString};
15+
use crate::snippet::{
16+
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
17+
};
1618
use crate::styled_buffer::StyledBuffer;
1719
use crate::translation::{to_fluent_args, Translate};
1820
use crate::{
@@ -858,7 +860,7 @@ impl EmitterWriter {
858860
let mut short_start = true;
859861
for ann in &line.annotations {
860862
if let AnnotationType::MultilineStart(depth) = ann.annotation_type {
861-
if source_string.chars().take(ann.start_col).all(|c| c.is_whitespace()) {
863+
if source_string.chars().take(ann.start_col.display).all(|c| c.is_whitespace()) {
862864
let style = if ann.is_primary {
863865
Style::UnderlinePrimary
864866
} else {
@@ -1093,15 +1095,15 @@ impl EmitterWriter {
10931095
'_',
10941096
line_offset + pos,
10951097
width_offset + depth,
1096-
(code_offset + annotation.start_col).saturating_sub(left),
1098+
(code_offset + annotation.start_col.display).saturating_sub(left),
10971099
style,
10981100
);
10991101
}
11001102
_ if self.teach => {
11011103
buffer.set_style_range(
11021104
line_offset,
1103-
(code_offset + annotation.start_col).saturating_sub(left),
1104-
(code_offset + annotation.end_col).saturating_sub(left),
1105+
(code_offset + annotation.start_col.display).saturating_sub(left),
1106+
(code_offset + annotation.end_col.display).saturating_sub(left),
11051107
style,
11061108
annotation.is_primary,
11071109
);
@@ -1133,7 +1135,7 @@ impl EmitterWriter {
11331135
for p in line_offset + 1..=line_offset + pos {
11341136
buffer.putc(
11351137
p,
1136-
(code_offset + annotation.start_col).saturating_sub(left),
1138+
(code_offset + annotation.start_col.display).saturating_sub(left),
11371139
'|',
11381140
style,
11391141
);
@@ -1169,9 +1171,9 @@ impl EmitterWriter {
11691171
let style =
11701172
if annotation.is_primary { Style::LabelPrimary } else { Style::LabelSecondary };
11711173
let (pos, col) = if pos == 0 {
1172-
(pos + 1, (annotation.end_col + 1).saturating_sub(left))
1174+
(pos + 1, (annotation.end_col.display + 1).saturating_sub(left))
11731175
} else {
1174-
(pos + 2, annotation.start_col.saturating_sub(left))
1176+
(pos + 2, annotation.start_col.display.saturating_sub(left))
11751177
};
11761178
if let Some(ref label) = annotation.label {
11771179
buffer.puts(line_offset + pos, code_offset + col, label, style);
@@ -1208,7 +1210,7 @@ impl EmitterWriter {
12081210
} else {
12091211
('-', Style::UnderlineSecondary)
12101212
};
1211-
for p in annotation.start_col..annotation.end_col {
1213+
for p in annotation.start_col.display..annotation.end_col.display {
12121214
buffer.putc(
12131215
line_offset + 1,
12141216
(code_offset + p).saturating_sub(left),
@@ -1459,7 +1461,7 @@ impl EmitterWriter {
14591461
&annotated_file.file.name,
14601462
line.line_index
14611463
),
1462-
annotations[0].start_col + 1,
1464+
annotations[0].start_col.file + 1,
14631465
),
14641466
Style::LineAndColumn,
14651467
);
@@ -1546,7 +1548,7 @@ impl EmitterWriter {
15461548
buffer.prepend(buffer_msg_line_offset + 1, "::: ", Style::LineNumber);
15471549
let loc = if let Some(first_line) = annotated_file.lines.first() {
15481550
let col = if let Some(first_annotation) = first_line.annotations.first() {
1549-
format!(":{}", first_annotation.start_col + 1)
1551+
format!(":{}", first_annotation.start_col.file + 1)
15501552
} else {
15511553
String::new()
15521554
};
@@ -1607,8 +1609,8 @@ impl EmitterWriter {
16071609
let mut span_left_margin = usize::MAX;
16081610
for line in &annotated_file.lines {
16091611
for ann in &line.annotations {
1610-
span_left_margin = min(span_left_margin, ann.start_col);
1611-
span_left_margin = min(span_left_margin, ann.end_col);
1612+
span_left_margin = min(span_left_margin, ann.start_col.display);
1613+
span_left_margin = min(span_left_margin, ann.end_col.display);
16121614
}
16131615
}
16141616
if span_left_margin == usize::MAX {
@@ -1625,11 +1627,12 @@ impl EmitterWriter {
16251627
annotated_file.file.get_line(line.line_index - 1).map_or(0, |s| s.len()),
16261628
);
16271629
for ann in &line.annotations {
1628-
span_right_margin = max(span_right_margin, ann.start_col);
1629-
span_right_margin = max(span_right_margin, ann.end_col);
1630+
span_right_margin = max(span_right_margin, ann.start_col.display);
1631+
span_right_margin = max(span_right_margin, ann.end_col.display);
16301632
// FIXME: account for labels not in the same line
16311633
let label_right = ann.label.as_ref().map_or(0, |l| l.len() + 1);
1632-
label_right_margin = max(label_right_margin, ann.end_col + label_right);
1634+
label_right_margin =
1635+
max(label_right_margin, ann.end_col.display + label_right);
16331636
}
16341637
}
16351638

@@ -2352,17 +2355,17 @@ impl FileWithAnnotatedLines {
23522355
depth: 1,
23532356
line_start: lo.line,
23542357
line_end: hi.line,
2355-
start_col: lo.col_display,
2356-
end_col: hi.col_display,
2358+
start_col: AnnotationColumn::from_loc(&lo),
2359+
end_col: AnnotationColumn::from_loc(&hi),
23572360
is_primary,
23582361
label,
23592362
overlaps_exactly: false,
23602363
};
23612364
multiline_annotations.push((lo.file, ml));
23622365
} else {
23632366
let ann = Annotation {
2364-
start_col: lo.col_display,
2365-
end_col: hi.col_display,
2367+
start_col: AnnotationColumn::from_loc(&lo),
2368+
end_col: AnnotationColumn::from_loc(&hi),
23662369
is_primary,
23672370
label,
23682371
annotation_type: AnnotationType::Singleline,
@@ -2551,7 +2554,13 @@ fn num_overlap(
25512554
(b_start..b_end + extra).contains(&a_start) || (a_start..a_end + extra).contains(&b_start)
25522555
}
25532556
fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
2554-
num_overlap(a1.start_col, a1.end_col + padding, a2.start_col, a2.end_col, false)
2557+
num_overlap(
2558+
a1.start_col.display,
2559+
a1.end_col.display + padding,
2560+
a2.start_col.display,
2561+
a2.end_col.display,
2562+
false,
2563+
)
25552564
}
25562565

25572566
fn emit_to_destination(

compiler/rustc_errors/src/snippet.rs

+51-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
// Code for annotating snippets.
22

3-
use crate::Level;
3+
use crate::{Level, Loc};
44

55
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
66
pub struct Line {
77
pub line_index: usize,
88
pub annotations: Vec<Annotation>,
99
}
1010

11+
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
12+
pub struct AnnotationColumn {
13+
/// the (0-indexed) column for *display* purposes, counted in characters, not utf-8 bytes
14+
pub display: usize,
15+
/// the (0-indexed) column in the file, counted in characters, not utf-8 bytes.
16+
///
17+
/// this may be different from `self.display`,
18+
/// e.g. if the file contains hard tabs, because we convert tabs to spaces for error messages.
19+
///
20+
/// for example:
21+
/// ```text
22+
/// (hard tab)hello
23+
/// ^ this is display column 4, but file column 1
24+
/// ```
25+
///
26+
/// we want to keep around the correct file offset so that column numbers in error messages
27+
/// are correct. (motivated by <https://github.com/rust-lang/rust/issues/109537>)
28+
pub file: usize,
29+
}
30+
31+
impl AnnotationColumn {
32+
pub fn from_loc(loc: &Loc) -> AnnotationColumn {
33+
AnnotationColumn { display: loc.col_display, file: loc.col.0 }
34+
}
35+
}
36+
1137
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
1238
pub struct MultilineAnnotation {
1339
pub depth: usize,
1440
pub line_start: usize,
1541
pub line_end: usize,
16-
pub start_col: usize,
17-
pub end_col: usize,
42+
pub start_col: AnnotationColumn,
43+
pub end_col: AnnotationColumn,
1844
pub is_primary: bool,
1945
pub label: Option<String>,
2046
pub overlaps_exactly: bool,
@@ -36,7 +62,12 @@ impl MultilineAnnotation {
3662
pub fn as_start(&self) -> Annotation {
3763
Annotation {
3864
start_col: self.start_col,
39-
end_col: self.start_col + 1,
65+
end_col: AnnotationColumn {
66+
// these might not correspond to the same place anymore,
67+
// but that's okay for our purposes
68+
display: self.start_col.display + 1,
69+
file: self.start_col.file + 1,
70+
},
4071
is_primary: self.is_primary,
4172
label: None,
4273
annotation_type: AnnotationType::MultilineStart(self.depth),
@@ -45,7 +76,12 @@ impl MultilineAnnotation {
4576

4677
pub fn as_end(&self) -> Annotation {
4778
Annotation {
48-
start_col: self.end_col.saturating_sub(1),
79+
start_col: AnnotationColumn {
80+
// these might not correspond to the same place anymore,
81+
// but that's okay for our purposes
82+
display: self.end_col.display.saturating_sub(1),
83+
file: self.end_col.file.saturating_sub(1),
84+
},
4985
end_col: self.end_col,
5086
is_primary: self.is_primary,
5187
label: self.label.clone(),
@@ -55,8 +91,8 @@ impl MultilineAnnotation {
5591

5692
pub fn as_line(&self) -> Annotation {
5793
Annotation {
58-
start_col: 0,
59-
end_col: 0,
94+
start_col: Default::default(),
95+
end_col: Default::default(),
6096
is_primary: self.is_primary,
6197
label: None,
6298
annotation_type: AnnotationType::MultilineLine(self.depth),
@@ -92,14 +128,14 @@ pub enum AnnotationType {
92128

93129
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
94130
pub struct Annotation {
95-
/// Start column, 0-based indexing -- counting *characters*, not
96-
/// utf-8 bytes. Note that it is important that this field goes
131+
/// Start column.
132+
/// Note that it is important that this field goes
97133
/// first, so that when we sort, we sort orderings by start
98134
/// column.
99-
pub start_col: usize,
135+
pub start_col: AnnotationColumn,
100136

101137
/// End column within the line (exclusive)
102-
pub end_col: usize,
138+
pub end_col: AnnotationColumn,
103139

104140
/// Is this annotation derived from primary span
105141
pub is_primary: bool,
@@ -118,12 +154,13 @@ impl Annotation {
118154
matches!(self.annotation_type, AnnotationType::MultilineLine(_))
119155
}
120156

157+
/// Length of this annotation as displayed in the stderr output
121158
pub fn len(&self) -> usize {
122159
// Account for usize underflows
123-
if self.end_col > self.start_col {
124-
self.end_col - self.start_col
160+
if self.end_col.display > self.start_col.display {
161+
self.end_col.display - self.start_col.display
125162
} else {
126-
self.start_col - self.end_col
163+
self.start_col.display - self.end_col.display
127164
}
128165
}
129166

compiler/rustc_hir_typeck/src/method/suggest.rs

+42-10
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245245
None
246246
}
247247

248+
fn suggest_missing_writer(
249+
&self,
250+
rcvr_ty: Ty<'tcx>,
251+
args: (&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>]),
252+
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
253+
let (ty_str, _ty_file) = self.tcx.short_ty_string(rcvr_ty);
254+
let mut err =
255+
struct_span_err!(self.tcx.sess, args.0.span, E0599, "cannot write into `{}`", ty_str);
256+
err.span_note(
257+
args.0.span,
258+
"must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method",
259+
);
260+
if let ExprKind::Lit(_) = args.0.kind {
261+
err.span_help(
262+
args.0.span.shrink_to_lo(),
263+
"a writer is needed before this format string",
264+
);
265+
};
266+
267+
err
268+
}
269+
248270
pub fn report_no_match_method_error(
249271
&self,
250272
mut span: Span,
@@ -323,16 +345,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
323345
}
324346
}
325347

326-
let mut err = struct_span_err!(
327-
tcx.sess,
328-
span,
329-
E0599,
330-
"no {} named `{}` found for {} `{}` in the current scope",
331-
item_kind,
332-
item_name,
333-
rcvr_ty.prefix_string(self.tcx),
334-
ty_str_reported,
335-
);
348+
let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.map_or(false, |def_id| {
349+
tcx.is_diagnostic_item(sym::write_macro, def_id)
350+
|| tcx.is_diagnostic_item(sym::writeln_macro, def_id)
351+
}) && item_name.name == Symbol::intern("write_fmt");
352+
let mut err = if is_write
353+
&& let Some(args) = args
354+
{
355+
self.suggest_missing_writer(rcvr_ty, args)
356+
} else {
357+
struct_span_err!(
358+
tcx.sess,
359+
span,
360+
E0599,
361+
"no {} named `{}` found for {} `{}` in the current scope",
362+
item_kind,
363+
item_name,
364+
rcvr_ty.prefix_string(self.tcx),
365+
ty_str_reported,
366+
)
367+
};
336368
if tcx.sess.source_map().is_multiline(sugg_span) {
337369
err.span_label(sugg_span.with_hi(span.lo()), "");
338370
}

0 commit comments

Comments
 (0)