Skip to content

Commit 9155a9d

Browse files
committedJan 8, 2021
Auto merge of #80806 - JohnTitor:rollup-y64z7ph, r=JohnTitor
Rollup of 7 pull requests Successful merges: - #79675 (Make sure rust-call errors occur correctly for traits) - #80372 (Don't panic when an external crate can't be resolved) - #80761 (handle generic trait methods in coverage-report tests) - #80785 (rustc_ast_pretty: Remove `PrintState::insert_extra_parens`) - #80791 (Fix type name in doc example for Iter and IterMut) - #80794 (Use Option::map_or instead of `.map(..).unwrap_or(..)`) - #80799 (Get rid of custom pretty-printing in rustdoc) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 92c625d + dec3dbd commit 9155a9d

File tree

16 files changed

+90
-120
lines changed

16 files changed

+90
-120
lines changed
 

‎compiler/rustc_ast_pretty/src/pprust/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ use rustc_ast as ast;
88
use rustc_ast::token::{Nonterminal, Token, TokenKind};
99
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1010

11-
pub fn nonterminal_to_string_no_extra_parens(nt: &Nonterminal) -> String {
12-
let state = State::without_insert_extra_parens();
13-
state.nonterminal_to_string(nt)
14-
}
15-
1611
pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
1712
State::new().nonterminal_to_string(nt)
1813
}

‎compiler/rustc_ast_pretty/src/pprust/state.rs

+2-26
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ pub struct State<'a> {
8888
comments: Option<Comments<'a>>,
8989
ann: &'a (dyn PpAnn + 'a),
9090
is_expanded: bool,
91-
// If `true`, additional parenthesis (separate from `ExprKind::Paren`)
92-
// are inserted to ensure that proper precedence is preserved
93-
// in the pretty-printed output.
94-
//
95-
// This is usually `true`, except when performing the pretty-print/reparse
96-
// check in `nt_to_tokenstream`
97-
insert_extra_parens: bool,
9891
}
9992

10093
crate const INDENT_UNIT: usize = 4;
@@ -115,7 +108,6 @@ pub fn print_crate<'a>(
115108
comments: Some(Comments::new(sm, filename, input)),
116109
ann,
117110
is_expanded,
118-
insert_extra_parens: true,
119111
};
120112

121113
if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) {
@@ -235,7 +227,6 @@ impl std::ops::DerefMut for State<'_> {
235227
}
236228

237229
pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
238-
fn insert_extra_parens(&self) -> bool;
239230
fn comments(&mut self) -> &mut Option<Comments<'a>>;
240231
fn print_ident(&mut self, ident: Ident);
241232
fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
@@ -819,16 +810,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
819810

820811
fn to_string(&self, f: impl FnOnce(&mut State<'_>)) -> String {
821812
let mut printer = State::new();
822-
printer.insert_extra_parens = self.insert_extra_parens();
823813
f(&mut printer);
824814
printer.s.eof()
825815
}
826816
}
827817

828818
impl<'a> PrintState<'a> for State<'a> {
829-
fn insert_extra_parens(&self) -> bool {
830-
self.insert_extra_parens
831-
}
832819
fn comments(&mut self) -> &mut Option<Comments<'a>> {
833820
&mut self.comments
834821
}
@@ -865,17 +852,7 @@ impl<'a> PrintState<'a> for State<'a> {
865852

866853
impl<'a> State<'a> {
867854
pub fn new() -> State<'a> {
868-
State {
869-
s: pp::mk_printer(),
870-
comments: None,
871-
ann: &NoAnn,
872-
is_expanded: false,
873-
insert_extra_parens: true,
874-
}
875-
}
876-
877-
pub(super) fn without_insert_extra_parens() -> State<'a> {
878-
State { insert_extra_parens: false, ..State::new() }
855+
State { s: pp::mk_printer(), comments: None, ann: &NoAnn, is_expanded: false }
879856
}
880857

881858
// Synthesizes a comment that was not textually present in the original source
@@ -1680,8 +1657,7 @@ impl<'a> State<'a> {
16801657
}
16811658

16821659
/// Prints `expr` or `(expr)` when `needs_par` holds.
1683-
fn print_expr_cond_paren(&mut self, expr: &ast::Expr, mut needs_par: bool) {
1684-
needs_par &= self.insert_extra_parens;
1660+
fn print_expr_cond_paren(&mut self, expr: &ast::Expr, needs_par: bool) {
16851661
if needs_par {
16861662
self.popen();
16871663
}

‎compiler/rustc_hir_pretty/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ impl std::ops::DerefMut for State<'_> {
138138
}
139139

140140
impl<'a> PrintState<'a> for State<'a> {
141-
fn insert_extra_parens(&self) -> bool {
142-
true
143-
}
144141
fn comments(&mut self) -> &mut Option<Comments<'a>> {
145142
&mut self.comments
146143
}

‎compiler/rustc_parse/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ pub fn nt_to_tokenstream(
283283
} else if matches!(synthesize_tokens, CanSynthesizeMissingTokens::Yes) {
284284
return fake_token_stream(sess, nt);
285285
} else {
286-
let pretty = rustc_ast_pretty::pprust::nonterminal_to_string_no_extra_parens(&nt);
287-
panic!("Missing tokens for nt {:?}", pretty);
286+
panic!("Missing tokens for nt {:?}", pprust::nonterminal_to_string(nt));
288287
}
289288
}
290289

‎compiler/rustc_typeck/src/check/check.rs

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ pub(super) fn check_fn<'a, 'tcx>(
103103
Node::ImplItem(hir::ImplItem {
104104
kind: hir::ImplItemKind::Fn(header, ..), ..
105105
}) => Some(header),
106+
Node::TraitItem(hir::TraitItem {
107+
kind: hir::TraitItemKind::Fn(header, ..),
108+
..
109+
}) => Some(header),
106110
// Closures are RustCall, but they tuple their arguments, so shouldn't be checked
107111
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => None,
108112
node => bug!("Item being checked wasn't a function/closure: {:?}", node),

‎library/core/src/slice/iter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn size_from_ptr<T>(_: *const T) -> usize {
5151
/// Basic usage:
5252
///
5353
/// ```
54-
/// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]):
54+
/// // First, we declare a type which has `iter` method to get the `Iter` struct (`&[usize]` here):
5555
/// let slice = &[1, 2, 3];
5656
///
5757
/// // Then, we iterate over it:
@@ -112,7 +112,7 @@ impl<'a, T> Iter<'a, T> {
112112
///
113113
/// ```
114114
/// // First, we declare a type which has the `iter` method to get the `Iter`
115-
/// // struct (&[usize here]):
115+
/// // struct (`&[usize]` here):
116116
/// let slice = &[1, 2, 3];
117117
///
118118
/// // Then, we get the iterator:
@@ -167,7 +167,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
167167
///
168168
/// ```
169169
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
170-
/// // struct (&[usize here]):
170+
/// // struct (`&[usize]` here):
171171
/// let mut slice = &mut [1, 2, 3];
172172
///
173173
/// // Then, we iterate over it and increment each element value:
@@ -246,7 +246,7 @@ impl<'a, T> IterMut<'a, T> {
246246
///
247247
/// ```
248248
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
249-
/// // struct (&[usize here]):
249+
/// // struct (`&[usize]` here):
250250
/// let mut slice = &mut [1, 2, 3];
251251
///
252252
/// {

‎src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
942942
.iter()
943943
.enumerate()
944944
.map(|(i, ty)| {
945-
let mut name = self.1.get(i).map(|ident| ident.name).unwrap_or(kw::Empty);
945+
let mut name = self.1.get(i).map_or(kw::Empty, |ident| ident.name);
946946
if name.is_empty() {
947947
name = kw::Underscore;
948948
}
@@ -963,7 +963,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
963963
.iter()
964964
.enumerate()
965965
.map(|(i, ty)| Argument {
966-
name: name_from_pat(&body.params[i].pat),
966+
name: Symbol::intern(&rustc_hir_pretty::param_to_string(&body.params[i])),
967967
type_: ty.clean(cx),
968968
})
969969
.collect(),
@@ -1001,7 +1001,7 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
10011001
.iter()
10021002
.map(|t| Argument {
10031003
type_: t.clean(cx),
1004-
name: names.next().map(|i| i.name).unwrap_or(kw::Empty),
1004+
name: names.next().map_or(kw::Empty, |i| i.name),
10051005
})
10061006
.collect(),
10071007
},

‎src/librustdoc/clean/utils.rs

-70
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,6 @@ crate fn strip_path(path: &Path) -> Path {
314314
Path { global: path.global, res: path.res, segments }
315315
}
316316

317-
crate fn qpath_to_string(p: &hir::QPath<'_>) -> String {
318-
let segments = match *p {
319-
hir::QPath::Resolved(_, ref path) => &path.segments,
320-
hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
321-
hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
322-
};
323-
324-
let mut s = String::new();
325-
for (i, seg) in segments.iter().enumerate() {
326-
if i > 0 {
327-
s.push_str("::");
328-
}
329-
if seg.ident.name != kw::PathRoot {
330-
s.push_str(&seg.ident.as_str());
331-
}
332-
}
333-
s
334-
}
335-
336317
crate fn build_deref_target_impls(cx: &DocContext<'_>, items: &[Item], ret: &mut Vec<Item>) {
337318
let tcx = cx.tcx;
338319

@@ -376,57 +357,6 @@ impl ToSource for rustc_span::Span {
376357
}
377358
}
378359

379-
crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
380-
use rustc_hir::*;
381-
debug!("trying to get a name from pattern: {:?}", p);
382-
383-
Symbol::intern(&match p.kind {
384-
PatKind::Wild => return kw::Underscore,
385-
PatKind::Binding(_, _, ident, _) => return ident.name,
386-
PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
387-
PatKind::Struct(ref name, ref fields, etc) => format!(
388-
"{} {{ {}{} }}",
389-
qpath_to_string(name),
390-
fields
391-
.iter()
392-
.map(|fp| format!("{}: {}", fp.ident, name_from_pat(&fp.pat)))
393-
.collect::<Vec<String>>()
394-
.join(", "),
395-
if etc { ", .." } else { "" }
396-
),
397-
PatKind::Or(ref pats) => pats
398-
.iter()
399-
.map(|p| name_from_pat(&**p).to_string())
400-
.collect::<Vec<String>>()
401-
.join(" | "),
402-
PatKind::Tuple(ref elts, _) => format!(
403-
"({})",
404-
elts.iter()
405-
.map(|p| name_from_pat(&**p).to_string())
406-
.collect::<Vec<String>>()
407-
.join(", ")
408-
),
409-
PatKind::Box(ref p) => return name_from_pat(&**p),
410-
PatKind::Ref(ref p, _) => return name_from_pat(&**p),
411-
PatKind::Lit(..) => {
412-
warn!(
413-
"tried to get argument name from PatKind::Lit, which is silly in function arguments"
414-
);
415-
return Symbol::intern("()");
416-
}
417-
PatKind::Range(..) => panic!(
418-
"tried to get argument name from PatKind::Range, \
419-
which is not allowed in function arguments"
420-
),
421-
PatKind::Slice(ref begin, ref mid, ref end) => {
422-
let begin = begin.iter().map(|p| name_from_pat(&**p).to_string());
423-
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
424-
let end = end.iter().map(|p| name_from_pat(&**p).to_string());
425-
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
426-
}
427-
})
428-
}
429-
430360
crate fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
431361
match n.val {
432362
ty::ConstKind::Unevaluated(def, _, promoted) => {

‎src/librustdoc/core.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,15 @@ crate fn create_resolver<'a>(
434434
sess.time("load_extern_crates", || {
435435
for extern_name in &extern_names {
436436
debug!("loading extern crate {}", extern_name);
437-
resolver
437+
if let Err(()) = resolver
438438
.resolve_str_path_error(
439439
DUMMY_SP,
440440
extern_name,
441441
TypeNS,
442442
LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
443-
)
444-
.unwrap_or_else(|()| {
445-
panic!("Unable to resolve external crate {}", extern_name)
446-
});
443+
) {
444+
warn!("unable to resolve external crate {} (do you have an unused `--extern` crate?)", extern_name)
445+
}
447446
}
448447
});
449448
});

‎src/test/run-make-fulldeps/coverage-reports/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ else
172172
# files are redundant, so there is no need to generate `expected_*.json` files or
173173
# compare actual JSON results.)
174174

175-
$(DIFF) --ignore-matching-lines='::<.*>.*:$$' \
175+
$(DIFF) --ignore-matching-lines='^ | .*::<.*>.*:$$' --ignore-matching-lines='^ | <.*>::.*:$$' \
176176
expected_show_coverage.$@.txt "$(TMPDIR)"/actual_show_coverage.$@.txt || \
177177
( grep -q '^\/\/ ignore-llvm-cov-show-diffs' $(SOURCEDIR)/$@.rs && \
178178
>&2 echo 'diff failed, but suppressed with `// ignore-llvm-cov-show-diffs` in $(SOURCEDIR)/$@.rs' \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-flags: --extern zip=whatever.rlib
2+
#![deny(broken_intra_doc_links)]
3+
/// See [zip] crate.
4+
//~^ ERROR unresolved
5+
pub struct ArrayZip;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unresolved link to `zip`
2+
--> $DIR/unused-extern-crate.rs:3:10
3+
|
4+
LL | /// See [zip] crate.
5+
| ^^^ no item named `zip` in scope
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-extern-crate.rs:2:9
9+
|
10+
LL | #![deny(broken_intra_doc_links)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
13+
14+
error: aborting due to previous error
15+

‎src/test/rustdoc-ui/range-pattern.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check-pass
2+
3+
fn func(0u8..=255: u8) {}

‎src/test/rustdoc/range-arg-pattern.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/fn.f.html
4+
// @has - '//*[@class="rust fn"]' 'pub fn f(0u8 ...255: u8)'
5+
pub fn f(0u8...255: u8) {}

‎src/test/ui/abi/issues/issue-22565-rust-call.rs

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
extern "rust-call" fn b(_i: i32) {}
44
//~^ ERROR A function with the "rust-call" ABI must take a single non-self argument that is a tuple
55

6+
trait Tr {
7+
extern "rust-call" fn a();
8+
9+
extern "rust-call" fn b() {}
10+
//~^ ERROR A function with the "rust-call" ABI must take a single non-self argument
11+
}
12+
13+
struct Foo;
14+
15+
impl Foo {
16+
extern "rust-call" fn bar() {}
17+
//~^ ERROR A function with the "rust-call" ABI must take a single non-self argument
18+
}
19+
20+
impl Tr for Foo {
21+
extern "rust-call" fn a() {}
22+
//~^ ERROR A function with the "rust-call" ABI must take a single non-self argument
23+
}
24+
625
fn main () {
726
b(10);
27+
28+
Foo::bar();
29+
30+
<Foo as Tr>::a();
31+
<Foo as Tr>::b();
832
}

‎src/test/ui/abi/issues/issue-22565-rust-call.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,23 @@ error: A function with the "rust-call" ABI must take a single non-self argument
44
LL | extern "rust-call" fn b(_i: i32) {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: A function with the "rust-call" ABI must take a single non-self argument that is a tuple
8+
--> $DIR/issue-22565-rust-call.rs:9:5
9+
|
10+
LL | extern "rust-call" fn b() {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: A function with the "rust-call" ABI must take a single non-self argument that is a tuple
14+
--> $DIR/issue-22565-rust-call.rs:16:5
15+
|
16+
LL | extern "rust-call" fn bar() {}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: A function with the "rust-call" ABI must take a single non-self argument that is a tuple
20+
--> $DIR/issue-22565-rust-call.rs:21:5
21+
|
22+
LL | extern "rust-call" fn a() {}
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
826

0 commit comments

Comments
 (0)