Skip to content

Commit f001287

Browse files
committed
Auto merge of #57747 - Centril:rollup, r=Centril
Rollup of 11 pull requests Successful merges: - #57107 (Add a regression test for mutating a non-mut #[thread_local]) - #57132 (Document that `-C opt-level=0` implies `-C debug-assertions`.) - #57212 (docs(rustc): Link to the book's source in rustc) - #57302 (Fix unused_assignments false positive) - #57350 (Better error note on unimplemented Index trait for string) - #57635 (use structured macro and path resolve suggestions) - #57650 (librustc_metadata: Pass a default value when unwrapping a span) - #57657 (Add regression test to close #53787) - #57658 (Two HIR tweaks) - #57720 (Fix suggestions given mulitple bad lifetimes) - #57725 (Use structured suggestion to surround struct literal with parenthesis) Failed merges: r? @ghost
2 parents c76f3c3 + 2a830e4 commit f001287

28 files changed

+302
-90
lines changed

src/doc/rustc/src/codegen-options/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ This flag lets you control debug information:
187187

188188
This flag lets you control the optimization level.
189189

190-
* `0`: no optimizations
190+
* `0`: no optimizations, also turn on `cfg(debug_assertions)`.
191191
* `1`: basic optimizations
192192
* `2`: some optimizations
193193
* `3`: all optimizations

src/doc/rustc/src/contributing.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Contributing to rustc
22

33
We'd love to have your help improving `rustc`! To that end, we've written [a
4-
whole book](https://rust-lang.github.io/rustc-guide/) on its
4+
whole book][rustc_guide] on its
55
internals, how it works, and how to get started working on it. To learn
66
more, you'll want to check that out.
7+
8+
If you would like to contribute to _this_ book, you can find its source in the
9+
rustc source at [src/doc/rustc][rustc_book].
10+
11+
[rustc_guide]: https://rust-lang.github.io/rustc-guide/
12+
[rustc_book]: https://github.com/rust-lang/rust/tree/master/src/doc/rustc

src/libcore/ops/index.rs

+30
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@
5151
/// ```
5252
#[lang = "index"]
5353
#[rustc_on_unimplemented(
54+
on(
55+
_Self="&str",
56+
note="you can use `.chars().nth()` or `.bytes().nth()`
57+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
58+
),
59+
on(
60+
_Self="str",
61+
note="you can use `.chars().nth()` or `.bytes().nth()`
62+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
63+
),
64+
on(
65+
_Self="std::string::String",
66+
note="you can use `.chars().nth()` or `.bytes().nth()`
67+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
68+
),
5469
message="the type `{Self}` cannot be indexed by `{Idx}`",
5570
label="`{Self}` cannot be indexed by `{Idx}`",
5671
)]
@@ -141,6 +156,21 @@ pub trait Index<Idx: ?Sized> {
141156
/// ```
142157
#[lang = "index_mut"]
143158
#[rustc_on_unimplemented(
159+
on(
160+
_Self="&str",
161+
note="you can use `.chars().nth()` or `.bytes().nth()`
162+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
163+
),
164+
on(
165+
_Self="str",
166+
note="you can use `.chars().nth()` or `.bytes().nth()`
167+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
168+
),
169+
on(
170+
_Self="std::string::String",
171+
note="you can use `.chars().nth()` or `.bytes().nth()`
172+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
173+
),
144174
message="the type `{Self}` cannot be mutably indexed by `{Idx}`",
145175
label="`{Self}` cannot be mutably indexed by `{Idx}`",
146176
)]

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3775,7 +3775,7 @@ impl<'a> LoweringContext<'a> {
37753775
let ohs = P(self.lower_expr(ohs));
37763776
hir::ExprKind::Unary(op, ohs)
37773777
}
3778-
ExprKind::Lit(ref l) => hir::ExprKind::Lit(P((*l).clone())),
3778+
ExprKind::Lit(ref l) => hir::ExprKind::Lit((*l).clone()),
37793779
ExprKind::Cast(ref expr, ref ty) => {
37803780
let expr = P(self.lower_expr(expr));
37813781
hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))

src/librustc/hir/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
1919
use syntax::source_map::{self, Spanned};
2020
use rustc_target::spec::abi::Abi;
2121
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
22-
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy};
22+
use syntax::ast::{Attribute, Label, Lit, StrStyle, FloatTy, IntTy, UintTy};
2323
use syntax::attr::InlineAttr;
2424
use syntax::ext::hygiene::SyntaxContext;
2525
use syntax::ptr::P;
@@ -142,17 +142,6 @@ pub const DUMMY_HIR_ID: HirId = HirId {
142142

143143
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
144144

145-
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
146-
pub struct Label {
147-
pub ident: Ident,
148-
}
149-
150-
impl fmt::Debug for Label {
151-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152-
write!(f, "label({:?})", self.ident)
153-
}
154-
}
155-
156145
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
157146
pub struct Lifetime {
158147
pub id: NodeId,
@@ -1466,7 +1455,7 @@ pub enum ExprKind {
14661455
/// A unary operation (For example: `!x`, `*x`)
14671456
Unary(UnOp, P<Expr>),
14681457
/// A literal (For example: `1`, `"foo"`)
1469-
Lit(P<Lit>),
1458+
Lit(Lit),
14701459
/// A cast (`foo as f64`)
14711460
Cast(P<Expr>, P<Ty>),
14721461
Type(P<Expr>, P<Ty>),

src/librustc/ich/impls_hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl_stable_hash_for!(enum hir::LifetimeName {
153153
Error,
154154
});
155155

156-
impl_stable_hash_for!(struct hir::Label {
156+
impl_stable_hash_for!(struct ast::Label {
157157
ident
158158
});
159159

src/librustc/middle/liveness.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
911911
}
912912

913913
fn compute(&mut self, body: &hir::Expr) -> LiveNode {
914-
// if there is a `break` or `again` at the top level, then it's
915-
// effectively a return---this only occurs in `for` loops,
916-
// where the body is really a closure.
917-
918914
debug!("compute: using id for body, {}", self.ir.tcx.hir().node_to_pretty_string(body.id));
919915

920-
let exit_ln = self.s.exit_ln;
921-
922-
self.break_ln.insert(body.id, exit_ln);
923-
self.cont_ln.insert(body.id, exit_ln);
924-
925916
// the fallthrough exit is only for those cases where we do not
926917
// explicitly return:
927918
let s = self.s;
@@ -1024,19 +1015,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10241015
self.propagate_through_expr(&e, succ)
10251016
}
10261017

1027-
hir::ExprKind::Closure(.., blk_id, _, _) => {
1018+
hir::ExprKind::Closure(..) => {
10281019
debug!("{} is an ExprKind::Closure",
10291020
self.ir.tcx.hir().node_to_pretty_string(expr.id));
10301021

1031-
// The next-node for a break is the successor of the entire
1032-
// loop. The next-node for a continue is the top of this loop.
1033-
let node = self.live_node(expr.hir_id, expr.span);
1034-
1035-
let break_ln = succ;
1036-
let cont_ln = node;
1037-
self.break_ln.insert(blk_id.node_id, break_ln);
1038-
self.cont_ln.insert(blk_id.node_id, cont_ln);
1039-
10401022
// the construction of a closure itself is not important,
10411023
// but we have to consider the closed over variables.
10421024
let caps = self.ir.capture_info_map.get(&expr.id).cloned().unwrap_or_else(||
@@ -1407,15 +1389,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14071389
debug!("propagate_through_loop: using id for loop body {} {}",
14081390
expr.id, self.ir.tcx.hir().node_to_pretty_string(body.id));
14091391

1410-
let break_ln = succ;
1411-
let cont_ln = ln;
1412-
self.break_ln.insert(expr.id, break_ln);
1413-
self.cont_ln.insert(expr.id, cont_ln);
1392+
1393+
self.break_ln.insert(expr.id, succ);
14141394

14151395
let cond_ln = match kind {
14161396
LoopLoop => ln,
14171397
WhileLoop(ref cond) => self.propagate_through_expr(&cond, ln),
14181398
};
1399+
1400+
self.cont_ln.insert(expr.id, cond_ln);
1401+
14191402
let body_ln = self.propagate_through_block(body, cond_ln);
14201403

14211404
// repeat until fixed point is reached:

src/librustc_errors/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ impl CodeSuggestion {
135135
if let Some(line) = line_opt {
136136
if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
137137
let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
138-
buf.push_str(match hi_opt {
139-
Some(hi) => &line[lo..hi],
140-
None => &line[lo..],
141-
});
138+
match hi_opt {
139+
Some(hi) if hi > lo => buf.push_str(&line[lo..hi]),
140+
Some(_) => (),
141+
None => buf.push_str(&line[lo..]),
142+
}
142143
}
143144
if let None = hi_opt {
144145
buf.push('\n');

src/librustc_metadata/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'a, 'tcx> Collector<'a, 'tcx> {
163163
!self.tcx.features().static_nobundle {
164164
feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
165165
"static_nobundle",
166-
span.unwrap(),
166+
span.unwrap_or_else(|| syntax_pos::DUMMY_SP),
167167
GateIssue::Language,
168168
"kind=\"static-nobundle\" is feature gated");
169169
}

src/librustc_resolve/lib.rs

+56-10
Original file line numberDiff line numberDiff line change
@@ -3318,7 +3318,12 @@ impl<'a> Resolver<'a> {
33183318
if let Some(def) = def {
33193319
match (def, source) {
33203320
(Def::Macro(..), _) => {
3321-
err.span_label(span, format!("did you mean `{}!(...)`?", path_str));
3321+
err.span_suggestion_with_applicability(
3322+
span,
3323+
"use `!` to invoke the macro",
3324+
format!("{}!", path_str),
3325+
Applicability::MaybeIncorrect,
3326+
);
33223327
return (err, candidates);
33233328
}
33243329
(Def::TyAlias(..), PathSource::Trait(_)) => {
@@ -3330,13 +3335,22 @@ impl<'a> Resolver<'a> {
33303335
}
33313336
(Def::Mod(..), PathSource::Expr(Some(parent))) => match parent.node {
33323337
ExprKind::Field(_, ident) => {
3333-
err.span_label(parent.span, format!("did you mean `{}::{}`?",
3334-
path_str, ident));
3338+
err.span_suggestion_with_applicability(
3339+
parent.span,
3340+
"use the path separator to refer to an item",
3341+
format!("{}::{}", path_str, ident),
3342+
Applicability::MaybeIncorrect,
3343+
);
33353344
return (err, candidates);
33363345
}
33373346
ExprKind::MethodCall(ref segment, ..) => {
3338-
err.span_label(parent.span, format!("did you mean `{}::{}(...)`?",
3339-
path_str, segment.ident));
3347+
let span = parent.span.with_hi(segment.ident.span.hi());
3348+
err.span_suggestion_with_applicability(
3349+
span,
3350+
"use the path separator to refer to an item",
3351+
format!("{}::{}", path_str, segment.ident),
3352+
Applicability::MaybeIncorrect,
3353+
);
33403354
return (err, candidates);
33413355
}
33423356
_ => {}
@@ -3387,6 +3401,29 @@ impl<'a> Resolver<'a> {
33873401
Ok(ref snippet) if snippet == "{" => true,
33883402
_ => false,
33893403
};
3404+
// In case this could be a struct literal that needs to be surrounded
3405+
// by parenthesis, find the appropriate span.
3406+
let mut i = 0;
3407+
let mut closing_brace = None;
3408+
loop {
3409+
sp = sm.next_point(sp);
3410+
match sm.span_to_snippet(sp) {
3411+
Ok(ref snippet) => {
3412+
if snippet == "}" {
3413+
let sp = span.to(sp);
3414+
if let Ok(snippet) = sm.span_to_snippet(sp) {
3415+
closing_brace = Some((sp, snippet));
3416+
}
3417+
break;
3418+
}
3419+
}
3420+
_ => break,
3421+
}
3422+
i += 1;
3423+
if i > 100 { // The bigger the span the more likely we're
3424+
break; // incorrect. Bound it to 100 chars long.
3425+
}
3426+
}
33903427
match source {
33913428
PathSource::Expr(Some(parent)) => {
33923429
match parent.node {
@@ -3413,11 +3450,20 @@ impl<'a> Resolver<'a> {
34133450
}
34143451
},
34153452
PathSource::Expr(None) if followed_by_brace == true => {
3416-
err.span_label(
3417-
span,
3418-
format!("did you mean `({} {{ /* fields */ }})`?",
3419-
path_str),
3420-
);
3453+
if let Some((sp, snippet)) = closing_brace {
3454+
err.span_suggestion_with_applicability(
3455+
sp,
3456+
"surround the struct literal with parenthesis",
3457+
format!("({})", snippet),
3458+
Applicability::MaybeIncorrect,
3459+
);
3460+
} else {
3461+
err.span_label(
3462+
span,
3463+
format!("did you mean `({} {{ /* fields */ }})`?",
3464+
path_str),
3465+
);
3466+
}
34213467
return (err, candidates);
34223468
},
34233469
_ => {

src/libsyntax/parse/parser.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -5234,22 +5234,13 @@ impl<'a> Parser<'a> {
52345234
kind: ast::GenericParamKind::Lifetime,
52355235
});
52365236
if let Some(sp) = seen_ty_param {
5237-
let param_span = self.prev_span;
5238-
let ate_comma = self.eat(&token::Comma);
5239-
let remove_sp = if ate_comma {
5240-
param_span.until(self.span)
5241-
} else {
5242-
last_comma_span.unwrap_or(param_span).to(param_span)
5243-
};
5244-
bad_lifetime_pos.push(param_span);
5245-
5246-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(param_span) {
5237+
let remove_sp = last_comma_span.unwrap_or(self.prev_span).to(self.prev_span);
5238+
bad_lifetime_pos.push(self.prev_span);
5239+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.prev_span) {
52475240
suggestions.push((remove_sp, String::new()));
5248-
suggestions.push((sp.shrink_to_lo(), format!("{}, ", snippet)));
5249-
}
5250-
if ate_comma {
5251-
last_comma_span = Some(self.prev_span);
5252-
continue
5241+
suggestions.push((
5242+
sp.shrink_to_lo(),
5243+
format!("{}, ", snippet)));
52535244
}
52545245
}
52555246
} else if self.check_ident() {

src/test/ui/error-codes/E0423.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@ error[E0423]: expected value, found struct `S`
2929
--> $DIR/E0423.rs:12:32
3030
|
3131
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
32-
| ^ did you mean `(S { /* fields */ })`?
32+
| ^---------------
33+
| |
34+
| help: surround the struct literal with parenthesis: `(S { x: 1, y: 2 })`
3335

3436
error[E0423]: expected value, found struct `T`
3537
--> $DIR/E0423.rs:15:8
3638
|
3739
LL | if T {} == T {} { println!("Ok"); }
38-
| ^ did you mean `(T { /* fields */ })`?
40+
| ^---
41+
| |
42+
| help: surround the struct literal with parenthesis: `(T {})`
3943

4044
error[E0423]: expected value, found struct `std::ops::Range`
4145
--> $DIR/E0423.rs:21:14
4246
|
4347
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
44-
| ^^^^^^^^^^^^^^^ did you mean `(std::ops::Range { /* fields */ })`?
48+
| ^^^^^^^^^^^^^^^----------------------
49+
| |
50+
| help: surround the struct literal with parenthesis: `(std::ops::Range { start: 0, end: 10 })`
4551

4652
error: aborting due to 7 previous errors
4753

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//~ ERROR kind="static-nobundle" is feature gated
2+
// Test the behavior of rustc when non-existent library is statically linked
3+
4+
// compile-flags: -l static-nobundle=nonexistent
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error[E0658]: kind="static-nobundle" is feature gated (see issue #37403)
2+
|
3+
= help: add #![feature(static_nobundle)] to the crate attributes to enable
4+
5+
error: aborting due to previous error
6+
7+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Regression test for Issue #53787: Fix ICE when creating a label in inline assembler with macros.
2+
3+
#![feature(asm)]
4+
5+
macro_rules! fake_jump {
6+
($id:expr) => {
7+
unsafe {
8+
asm!(
9+
"
10+
jmp $0
11+
lea eax, [ebx]
12+
xor eax, 0xDEADBEEF
13+
retn
14+
$0:
15+
"::"0"($id)::"volatile", "intel");
16+
}
17+
};
18+
}
19+
20+
fn main() {
21+
fake_jump!("FirstFunc"); //~ ERROR invalid value for constraint in inline assembly
22+
println!("Hello, world!");
23+
}

0 commit comments

Comments
 (0)