Skip to content

Commit 5257aee

Browse files
committed
Auto merge of #121890 - matthiaskrgr:rollup-mv26uwt, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #109263 (fix typo in documentation for std::fs::Permissions) - #120684 (Update E0716.md for clarity) - #121715 (match lowering: pre-simplify or-patterns too) - #121739 (Display short types for unimplemented trait) - #121815 (Move `gather_comments`.) - #121835 (Move `HandleStore` into `server.rs`.) - #121847 (Remove hidden use of Global) - #121861 (Use the guaranteed precision of a couple of float functions in docs) - #121875 ( Account for unmet T: !Copy in E0277 message) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2e3581b + 07bd459 commit 5257aee

File tree

23 files changed

+546
-430
lines changed

23 files changed

+546
-430
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,7 @@ version = "0.0.0"
34913491
dependencies = [
34923492
"itertools 0.11.0",
34933493
"rustc_ast",
3494+
"rustc_lexer",
34943495
"rustc_span",
34953496
"thin-vec",
34963497
]
+1-125
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::token::CommentKind;
2-
use rustc_span::source_map::SourceMap;
3-
use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol};
2+
use rustc_span::{BytePos, Symbol};
43

54
#[cfg(test)]
65
mod tests;
@@ -131,126 +130,3 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
131130
}
132131
data
133132
}
134-
135-
/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
136-
/// Otherwise returns `Some(k)` where `k` is first char offset after that leading
137-
/// whitespace. Note that `k` may be outside bounds of `s`.
138-
fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
139-
let mut idx = 0;
140-
for (i, ch) in s.char_indices().take(col.to_usize()) {
141-
if !ch.is_whitespace() {
142-
return None;
143-
}
144-
idx = i + ch.len_utf8();
145-
}
146-
Some(idx)
147-
}
148-
149-
fn trim_whitespace_prefix(s: &str, col: CharPos) -> &str {
150-
let len = s.len();
151-
match all_whitespace(s, col) {
152-
Some(col) => {
153-
if col < len {
154-
&s[col..]
155-
} else {
156-
""
157-
}
158-
}
159-
None => s,
160-
}
161-
}
162-
163-
fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
164-
let mut res: Vec<String> = vec![];
165-
let mut lines = text.lines();
166-
// just push the first line
167-
res.extend(lines.next().map(|it| it.to_string()));
168-
// for other lines, strip common whitespace prefix
169-
for line in lines {
170-
res.push(trim_whitespace_prefix(line, col).to_string())
171-
}
172-
res
173-
}
174-
175-
// it appears this function is called only from pprust... that's
176-
// probably not a good thing.
177-
pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
178-
let sm = SourceMap::new(sm.path_mapping().clone());
179-
let source_file = sm.new_source_file(path, src);
180-
let text = (*source_file.src.as_ref().unwrap()).clone();
181-
182-
let text: &str = text.as_str();
183-
let start_bpos = source_file.start_pos;
184-
let mut pos = 0;
185-
let mut comments: Vec<Comment> = Vec::new();
186-
let mut code_to_the_left = false;
187-
188-
if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
189-
comments.push(Comment {
190-
style: CommentStyle::Isolated,
191-
lines: vec![text[..shebang_len].to_string()],
192-
pos: start_bpos,
193-
});
194-
pos += shebang_len;
195-
}
196-
197-
for token in rustc_lexer::tokenize(&text[pos..]) {
198-
let token_text = &text[pos..pos + token.len as usize];
199-
match token.kind {
200-
rustc_lexer::TokenKind::Whitespace => {
201-
if let Some(mut idx) = token_text.find('\n') {
202-
code_to_the_left = false;
203-
while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
204-
idx += 1 + next_newline;
205-
comments.push(Comment {
206-
style: CommentStyle::BlankLine,
207-
lines: vec![],
208-
pos: start_bpos + BytePos((pos + idx) as u32),
209-
});
210-
}
211-
}
212-
}
213-
rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
214-
if doc_style.is_none() {
215-
let code_to_the_right = !matches!(
216-
text[pos + token.len as usize..].chars().next(),
217-
Some('\r' | '\n')
218-
);
219-
let style = match (code_to_the_left, code_to_the_right) {
220-
(_, true) => CommentStyle::Mixed,
221-
(false, false) => CommentStyle::Isolated,
222-
(true, false) => CommentStyle::Trailing,
223-
};
224-
225-
// Count the number of chars since the start of the line by rescanning.
226-
let pos_in_file = start_bpos + BytePos(pos as u32);
227-
let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
228-
let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
229-
let col = CharPos(text[line_begin_pos..pos].chars().count());
230-
231-
let lines = split_block_comment_into_lines(token_text, col);
232-
comments.push(Comment { style, lines, pos: pos_in_file })
233-
}
234-
}
235-
rustc_lexer::TokenKind::LineComment { doc_style } => {
236-
if doc_style.is_none() {
237-
comments.push(Comment {
238-
style: if code_to_the_left {
239-
CommentStyle::Trailing
240-
} else {
241-
CommentStyle::Isolated
242-
},
243-
lines: vec![token_text.to_string()],
244-
pos: start_bpos + BytePos(pos as u32),
245-
})
246-
}
247-
}
248-
_ => {
249-
code_to_the_left = true;
250-
}
251-
}
252-
pos += token.len as usize;
253-
}
254-
255-
comments
256-
}

compiler/rustc_ast_pretty/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# tidy-alphabetical-start
88
itertools = "0.11"
99
rustc_ast = { path = "../rustc_ast" }
10+
rustc_lexer = { path = "../rustc_lexer" }
1011
rustc_span = { path = "../rustc_span" }
1112
thin-vec = "0.2.12"
1213
# tidy-alphabetical-end

compiler/rustc_ast_pretty/src/pprust/state.rs

+123-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::ptr::P;
1414
use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind};
1515
use rustc_ast::tokenstream::{Spacing, TokenStream, TokenTree};
1616
use rustc_ast::util::classify;
17-
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
17+
use rustc_ast::util::comments::{Comment, CommentStyle};
1818
use rustc_ast::util::parser;
1919
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, PatKind};
2020
use rustc_ast::{attr, BindingAnnotation, ByRef, DelimArgs, RangeEnd, RangeSyntax, Term};
@@ -24,7 +24,7 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
2424
use rustc_span::edition::Edition;
2525
use rustc_span::source_map::{SourceMap, Spanned};
2626
use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol};
27-
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
27+
use rustc_span::{BytePos, CharPos, FileName, Pos, Span, DUMMY_SP};
2828
use std::borrow::Cow;
2929
use thin_vec::ThinVec;
3030

@@ -59,6 +59,127 @@ pub struct Comments<'a> {
5959
current: usize,
6060
}
6161

62+
/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
63+
/// Otherwise returns `Some(k)` where `k` is first char offset after that leading
64+
/// whitespace. Note that `k` may be outside bounds of `s`.
65+
fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
66+
let mut idx = 0;
67+
for (i, ch) in s.char_indices().take(col.to_usize()) {
68+
if !ch.is_whitespace() {
69+
return None;
70+
}
71+
idx = i + ch.len_utf8();
72+
}
73+
Some(idx)
74+
}
75+
76+
fn trim_whitespace_prefix(s: &str, col: CharPos) -> &str {
77+
let len = s.len();
78+
match all_whitespace(s, col) {
79+
Some(col) => {
80+
if col < len {
81+
&s[col..]
82+
} else {
83+
""
84+
}
85+
}
86+
None => s,
87+
}
88+
}
89+
90+
fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
91+
let mut res: Vec<String> = vec![];
92+
let mut lines = text.lines();
93+
// just push the first line
94+
res.extend(lines.next().map(|it| it.to_string()));
95+
// for other lines, strip common whitespace prefix
96+
for line in lines {
97+
res.push(trim_whitespace_prefix(line, col).to_string())
98+
}
99+
res
100+
}
101+
102+
fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
103+
let sm = SourceMap::new(sm.path_mapping().clone());
104+
let source_file = sm.new_source_file(path, src);
105+
let text = (*source_file.src.as_ref().unwrap()).clone();
106+
107+
let text: &str = text.as_str();
108+
let start_bpos = source_file.start_pos;
109+
let mut pos = 0;
110+
let mut comments: Vec<Comment> = Vec::new();
111+
let mut code_to_the_left = false;
112+
113+
if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
114+
comments.push(Comment {
115+
style: CommentStyle::Isolated,
116+
lines: vec![text[..shebang_len].to_string()],
117+
pos: start_bpos,
118+
});
119+
pos += shebang_len;
120+
}
121+
122+
for token in rustc_lexer::tokenize(&text[pos..]) {
123+
let token_text = &text[pos..pos + token.len as usize];
124+
match token.kind {
125+
rustc_lexer::TokenKind::Whitespace => {
126+
if let Some(mut idx) = token_text.find('\n') {
127+
code_to_the_left = false;
128+
while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
129+
idx += 1 + next_newline;
130+
comments.push(Comment {
131+
style: CommentStyle::BlankLine,
132+
lines: vec![],
133+
pos: start_bpos + BytePos((pos + idx) as u32),
134+
});
135+
}
136+
}
137+
}
138+
rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
139+
if doc_style.is_none() {
140+
let code_to_the_right = !matches!(
141+
text[pos + token.len as usize..].chars().next(),
142+
Some('\r' | '\n')
143+
);
144+
let style = match (code_to_the_left, code_to_the_right) {
145+
(_, true) => CommentStyle::Mixed,
146+
(false, false) => CommentStyle::Isolated,
147+
(true, false) => CommentStyle::Trailing,
148+
};
149+
150+
// Count the number of chars since the start of the line by rescanning.
151+
let pos_in_file = start_bpos + BytePos(pos as u32);
152+
let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
153+
let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
154+
let col = CharPos(text[line_begin_pos..pos].chars().count());
155+
156+
let lines = split_block_comment_into_lines(token_text, col);
157+
comments.push(Comment { style, lines, pos: pos_in_file })
158+
}
159+
}
160+
rustc_lexer::TokenKind::LineComment { doc_style } => {
161+
if doc_style.is_none() {
162+
comments.push(Comment {
163+
style: if code_to_the_left {
164+
CommentStyle::Trailing
165+
} else {
166+
CommentStyle::Isolated
167+
},
168+
lines: vec![token_text.to_string()],
169+
pos: start_bpos + BytePos(pos as u32),
170+
})
171+
}
172+
}
173+
_ => {
174+
code_to_the_left = true;
175+
}
176+
}
177+
pos += token.len as usize;
178+
}
179+
180+
comments
181+
}
182+
62183
impl<'a> Comments<'a> {
63184
pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
64185
let comments = gather_comments(sm, filename, input);

compiler/rustc_error_codes/src/error_codes/E0716.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let q = p;
3030

3131
Whenever a temporary is created, it is automatically dropped (freed) according
3232
to fixed rules. Ordinarily, the temporary is dropped at the end of the enclosing
33-
statement -- in this case, after the `let`. This is illustrated in the example
33+
statement -- in this case, after the `let p`. This is illustrated in the example
3434
above by showing that `tmp` would be freed as we exit the block.
3535

3636
To fix this problem, you need to create a local variable to store the value in

compiler/rustc_hir_typeck/src/method/suggest.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10491049
bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
10501050
let actual_prefix = rcvr_ty.prefix_string(self.tcx);
10511051
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
1052+
let mut long_ty_file = None;
10521053
let (primary_message, label) = if unimplemented_traits.len() == 1
10531054
&& unimplemented_traits_only
10541055
{
@@ -1061,8 +1062,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611062
// Avoid crashing.
10621063
return (None, None);
10631064
}
1064-
let OnUnimplementedNote { message, label, .. } =
1065-
self.err_ctxt().on_unimplemented_note(trait_ref, &obligation);
1065+
let OnUnimplementedNote { message, label, .. } = self
1066+
.err_ctxt()
1067+
.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
10661068
(message, label)
10671069
})
10681070
.unwrap()
@@ -1076,6 +1078,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10761078
)
10771079
});
10781080
err.primary_message(primary_message);
1081+
if let Some(file) = long_ty_file {
1082+
err.note(format!(
1083+
"the full name for the type has been written to '{}'",
1084+
file.display(),
1085+
));
1086+
err.note(
1087+
"consider using `--verbose` to print the full type name to the console",
1088+
);
1089+
}
10791090
if let Some(label) = label {
10801091
custom_span_label = true;
10811092
err.span_label(span, label);

0 commit comments

Comments
 (0)