Skip to content

Commit 44636ca

Browse files
authored
Unrolled build for rust-lang#119624
Rollup merge of rust-lang#119624 - petrochenkov:dialoc4, r=compiler-errors rustc_span: More consistent span combination operations Also add more tests for using `tt` in addition to `ident`, and some other minor tweaks, see individual commits. This is a part of rust-lang#119412 that doesn't yet add side tables for metavariable spans.
2 parents 9212108 + 508d1ff commit 44636ca

25 files changed

+214
-98
lines changed

compiler/rustc_parse/src/parser/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ impl<'a> Parser<'a> {
24892489
}
24902490
ExprKind::Block(_, None) => {
24912491
this.dcx().emit_err(errors::IfExpressionMissingCondition {
2492-
if_span: lo.shrink_to_hi(),
2492+
if_span: lo.with_neighbor(cond.span).shrink_to_hi(),
24932493
block_span: self.sess.source_map().start_point(cond_span),
24942494
});
24952495
std::mem::replace(&mut cond, this.mk_expr_err(cond_span.shrink_to_hi()))
@@ -3735,7 +3735,7 @@ impl<'a> Parser<'a> {
37353735
}
37363736

37373737
pub(crate) fn mk_expr(&self, span: Span, kind: ExprKind) -> P<Expr> {
3738-
P(Expr { kind, span, attrs: AttrVec::new(), id: DUMMY_NODE_ID, tokens: None })
3738+
self.mk_expr_with_attrs(span, kind, AttrVec::new())
37393739
}
37403740

37413741
pub(super) fn mk_expr_err(&self, span: Span) -> P<Expr> {

compiler/rustc_parse/src/parser/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ impl<'a> Parser<'a> {
21182118
Applicability::MaybeIncorrect,
21192119
);
21202120
err.span_suggestion(
2121-
span.shrink_to_hi(),
2121+
span.with_neighbor(self.token.span).shrink_to_hi(),
21222122
"add a semicolon",
21232123
';',
21242124
Applicability::MaybeIncorrect,
@@ -2632,7 +2632,7 @@ impl<'a> Parser<'a> {
26322632

26332633
let is_name_required = match this.token.kind {
26342634
token::DotDotDot => false,
2635-
_ => req_name(this.token.span.edition()),
2635+
_ => req_name(this.token.span.with_neighbor(this.prev_token.span).edition()),
26362636
};
26372637
let (pat, ty) = if is_name_required || this.is_named_param() {
26382638
debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);

compiler/rustc_span/src/hygiene.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -852,21 +852,6 @@ impl fmt::Debug for SyntaxContext {
852852
}
853853

854854
impl Span {
855-
/// Creates a fresh expansion with given properties.
856-
/// Expansions are normally created by macros, but in some cases expansions are created for
857-
/// other compiler-generated code to set per-span properties like allowed unstable features.
858-
/// The returned span belongs to the created expansion and has the new properties,
859-
/// but its location is inherited from the current span.
860-
pub fn fresh_expansion(self, expn_id: LocalExpnId) -> Span {
861-
HygieneData::with(|data| {
862-
self.with_ctxt(data.apply_mark(
863-
self.ctxt(),
864-
expn_id.to_expn_id(),
865-
Transparency::Transparent,
866-
))
867-
})
868-
}
869-
870855
/// Reuses the span but adds information like the kind of the desugaring and features that are
871856
/// allowed inside this span.
872857
pub fn mark_with_reason(
@@ -881,7 +866,7 @@ impl Span {
881866
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
882867
};
883868
let expn_id = LocalExpnId::fresh(expn_data, ctx);
884-
self.fresh_expansion(expn_id)
869+
self.apply_mark(expn_id.to_expn_id(), Transparency::Transparent)
885870
}
886871
}
887872

compiler/rustc_span/src/lib.rs

+47-49
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,39 @@ impl Span {
825825
)
826826
}
827827

828+
/// Prepare two spans to a combine operation like `to` or `between`.
829+
/// FIXME: consider using declarative macro metavariable spans for the given spans if they are
830+
/// better suitable for combining (#119412).
831+
fn prepare_to_combine(
832+
a_orig: Span,
833+
b_orig: Span,
834+
) -> Result<(SpanData, SpanData, Option<LocalDefId>), Span> {
835+
let (a, b) = (a_orig.data(), b_orig.data());
836+
837+
if a.ctxt != b.ctxt {
838+
// Context mismatches usually happen when procedural macros combine spans copied from
839+
// the macro input with spans produced by the macro (`Span::*_site`).
840+
// In that case we consider the combined span to be produced by the macro and return
841+
// the original macro-produced span as the result.
842+
// Otherwise we just fall back to returning the first span.
843+
// Combining locations typically doesn't make sense in case of context mismatches.
844+
// `is_root` here is a fast path optimization.
845+
let a_is_callsite = a.ctxt.is_root() || a.ctxt == b.span().source_callsite().ctxt();
846+
return Err(if a_is_callsite { b_orig } else { a_orig });
847+
}
848+
849+
let parent = if a.parent == b.parent { a.parent } else { None };
850+
Ok((a, b, parent))
851+
}
852+
853+
/// This span, but in a larger context, may switch to the metavariable span if suitable.
854+
pub fn with_neighbor(self, neighbor: Span) -> Span {
855+
match Span::prepare_to_combine(self, neighbor) {
856+
Ok((this, ..)) => Span::new(this.lo, this.hi, this.ctxt, this.parent),
857+
Err(_) => self,
858+
}
859+
}
860+
828861
/// Returns a `Span` that would enclose both `self` and `end`.
829862
///
830863
/// Note that this can also be used to extend the span "backwards":
@@ -836,26 +869,12 @@ impl Span {
836869
/// ^^^^^^^^^^^^^^^^^^^^
837870
/// ```
838871
pub fn to(self, end: Span) -> Span {
839-
let span_data = self.data();
840-
let end_data = end.data();
841-
// FIXME(jseyfried): `self.ctxt` should always equal `end.ctxt` here (cf. issue #23480).
842-
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
843-
// have an incomplete span than a completely nonsensical one.
844-
if span_data.ctxt != end_data.ctxt {
845-
if span_data.ctxt.is_root() {
846-
return end;
847-
} else if end_data.ctxt.is_root() {
848-
return self;
872+
match Span::prepare_to_combine(self, end) {
873+
Ok((from, to, parent)) => {
874+
Span::new(cmp::min(from.lo, to.lo), cmp::max(from.hi, to.hi), from.ctxt, parent)
849875
}
850-
// Both spans fall within a macro.
851-
// FIXME(estebank): check if it is the *same* macro.
876+
Err(fallback) => fallback,
852877
}
853-
Span::new(
854-
cmp::min(span_data.lo, end_data.lo),
855-
cmp::max(span_data.hi, end_data.hi),
856-
if span_data.ctxt.is_root() { end_data.ctxt } else { span_data.ctxt },
857-
if span_data.parent == end_data.parent { span_data.parent } else { None },
858-
)
859878
}
860879

861880
/// Returns a `Span` between the end of `self` to the beginning of `end`.
@@ -866,14 +885,12 @@ impl Span {
866885
/// ^^^^^^^^^^^^^
867886
/// ```
868887
pub fn between(self, end: Span) -> Span {
869-
let span = self.data();
870-
let end = end.data();
871-
Span::new(
872-
span.hi,
873-
end.lo,
874-
if end.ctxt.is_root() { end.ctxt } else { span.ctxt },
875-
if span.parent == end.parent { span.parent } else { None },
876-
)
888+
match Span::prepare_to_combine(self, end) {
889+
Ok((from, to, parent)) => {
890+
Span::new(cmp::min(from.hi, to.hi), cmp::max(from.lo, to.lo), from.ctxt, parent)
891+
}
892+
Err(fallback) => fallback,
893+
}
877894
}
878895

879896
/// Returns a `Span` from the beginning of `self` until the beginning of `end`.
@@ -884,31 +901,12 @@ impl Span {
884901
/// ^^^^^^^^^^^^^^^^^
885902
/// ```
886903
pub fn until(self, end: Span) -> Span {
887-
// Most of this function's body is copied from `to`.
888-
// We can't just do `self.to(end.shrink_to_lo())`,
889-
// because to also does some magic where it uses min/max so
890-
// it can handle overlapping spans. Some advanced mis-use of
891-
// `until` with different ctxts makes this visible.
892-
let span_data = self.data();
893-
let end_data = end.data();
894-
// FIXME(jseyfried): `self.ctxt` should always equal `end.ctxt` here (cf. issue #23480).
895-
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
896-
// have an incomplete span than a completely nonsensical one.
897-
if span_data.ctxt != end_data.ctxt {
898-
if span_data.ctxt.is_root() {
899-
return end;
900-
} else if end_data.ctxt.is_root() {
901-
return self;
904+
match Span::prepare_to_combine(self, end) {
905+
Ok((from, to, parent)) => {
906+
Span::new(cmp::min(from.lo, to.lo), cmp::max(from.lo, to.lo), from.ctxt, parent)
902907
}
903-
// Both spans fall within a macro.
904-
// FIXME(estebank): check if it is the *same* macro.
908+
Err(fallback) => fallback,
905909
}
906-
Span::new(
907-
span_data.lo,
908-
end_data.lo,
909-
if end_data.ctxt.is_root() { end_data.ctxt } else { span_data.ctxt },
910-
if span_data.parent == end_data.parent { span_data.parent } else { None },
911-
)
912910
}
913911

914912
pub fn from_inner(self, inner: InnerSpan) -> Span {

library/core/src/intrinsics/mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
//! ```rust
6666
//! #![feature(core_intrinsics, custom_mir)]
6767
//! #![allow(internal_features)]
68+
//! #![allow(unused_assignments)]
6869
//!
6970
//! use core::intrinsics::mir::*;
7071
//!

src/librustdoc/passes/lint/check_code_block_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{
88
use rustc_parse::parse_stream_from_source_str;
99
use rustc_resolve::rustdoc::source_span_for_markdown_range;
1010
use rustc_session::parse::ParseSess;
11-
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
11+
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, Transparency};
1212
use rustc_span::source_map::{FilePathMapping, SourceMap};
1313
use rustc_span::{FileName, InnerSpan, DUMMY_SP};
1414

@@ -50,7 +50,7 @@ fn check_rust_syntax(
5050
let expn_data =
5151
ExpnData::default(ExpnKind::AstPass(AstPass::TestHarness), DUMMY_SP, edition, None, None);
5252
let expn_id = cx.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx));
53-
let span = DUMMY_SP.fresh_expansion(expn_id);
53+
let span = DUMMY_SP.apply_mark(expn_id.to_expn_id(), Transparency::Transparent);
5454

5555
let is_empty = rustc_driver::catch_fatal_errors(|| {
5656
parse_stream_from_source_str(
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// check-pass
21
// edition:2018
32
// aux-build:anon-params-edition-hygiene.rs
43

@@ -8,6 +7,8 @@
87
#[macro_use]
98
extern crate anon_params_edition_hygiene;
109

11-
generate_trait_2015!(u8);
10+
generate_trait_2015_ident!(u8);
11+
// FIXME: Edition hygiene doesn't work correctly with `tt`s in this case.
12+
generate_trait_2015_tt!(u8); //~ ERROR expected one of `:`, `@`, or `|`, found `)`
1213

1314
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: expected one of `:`, `@`, or `|`, found `)`
2+
--> $DIR/anon-params-edition-hygiene.rs:12:1
3+
|
4+
LL | generate_trait_2015_tt!(u8);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `:`, `@`, or `|`
6+
|
7+
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
8+
= note: this error originates in the macro `generate_trait_2015_tt` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
help: if this is a `self` type, give it a parameter name
10+
|
11+
LL | generate_trait_2015_tt!(self: u8);
12+
| +++++
13+
help: if this is a parameter name, give it a type
14+
|
15+
LL | generate_trait_2015_tt!(u8: TypeName);
16+
| ++++++++++
17+
help: if this is a type, explicitly ignore the parameter name
18+
|
19+
LL | generate_trait_2015_tt!(_: u8);
20+
| ++
21+
22+
error: aborting due to 1 previous error
23+

tests/ui/anon-params/auxiliary/anon-params-edition-hygiene.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// edition:2015
22

33
#[macro_export]
4-
macro_rules! generate_trait_2015 {
4+
macro_rules! generate_trait_2015_ident {
55
($Type: ident) => {
6-
trait Trait {
6+
trait Trait1 {
7+
fn method($Type) {}
8+
}
9+
};
10+
}
11+
12+
#[macro_export]
13+
macro_rules! generate_trait_2015_tt {
14+
($Type: tt) => {
15+
trait Trait2 {
716
fn method($Type) {}
817
}
918
};

tests/ui/editions/auxiliary/edition-kw-macro-2015.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ macro_rules! consumes_async_raw {
2626
macro_rules! passes_ident {
2727
($i: ident) => ($i)
2828
}
29+
30+
#[macro_export]
31+
macro_rules! passes_tt {
32+
($i: tt) => ($i)
33+
}

tests/ui/editions/auxiliary/edition-kw-macro-2018.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ macro_rules! consumes_async_raw {
2626
macro_rules! passes_ident {
2727
($i: ident) => ($i)
2828
}
29+
30+
#[macro_export]
31+
macro_rules! passes_tt {
32+
($i: tt) => ($i)
33+
}

tests/ui/editions/edition-keywords-2015-2015-parsing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn check_async() {
1919

2020
if passes_ident!(async) == 1 {} // OK
2121
if passes_ident!(r#async) == 1 {} // OK
22+
if passes_tt!(async) == 1 {} // OK
23+
if passes_tt!(r#async) == 1 {} // OK
2224
module::async(); // OK
2325
module::r#async(); // OK
2426
}

tests/ui/editions/edition-keywords-2015-2015.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn check_async() {
2020

2121
if passes_ident!(async) == 1 {} // OK
2222
if passes_ident!(r#async) == 1 {} // OK
23+
if passes_tt!(async) == 1 {} // OK
24+
if passes_tt!(r#async) == 1 {} // OK
2325
one_async::async(); // OK
2426
one_async::r#async(); // OK
2527
two_async::async(); // OK

tests/ui/editions/edition-keywords-2015-2018-parsing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn check_async() {
1919

2020
if passes_ident!(async) == 1 {} // OK
2121
if passes_ident!(r#async) == 1 {} // OK
22+
if passes_tt!(async) == 1 {} // OK
23+
if passes_tt!(r#async) == 1 {} // OK
2224
module::async(); // OK
2325
module::r#async(); // OK
2426
}

tests/ui/editions/edition-keywords-2015-2018.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn check_async() {
2020

2121
if passes_ident!(async) == 1 {} // OK
2222
if passes_ident!(r#async) == 1 {} // OK
23+
if passes_tt!(async) == 1 {} // OK
24+
if passes_tt!(r#async) == 1 {} // OK
2325
// one_async::async(); // ERROR, unresolved name
2426
// one_async::r#async(); // ERROR, unresolved name
2527
two_async::async(); // OK

tests/ui/editions/edition-keywords-2018-2015-parsing.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ pub fn check_async() {
2121
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
2222
r#async = consumes_async_raw!(r#async); // OK
2323

24-
if passes_ident!(async) == 1 {}
24+
if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
2525
if passes_ident!(r#async) == 1 {} // OK
26+
if passes_tt!(async) == 1 {} //~ ERROR macro expansion ends with an incomplete expression
27+
if passes_tt!(r#async) == 1 {} // OK
2628
module::async(); //~ ERROR expected identifier, found keyword `async`
2729
module::r#async(); // OK
2830

tests/ui/editions/edition-keywords-2018-2015-parsing.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | let mut r#async = 1;
1010
| ++
1111

1212
error: expected identifier, found keyword `async`
13-
--> $DIR/edition-keywords-2018-2015-parsing.rs:26:13
13+
--> $DIR/edition-keywords-2018-2015-parsing.rs:28:13
1414
|
1515
LL | module::async();
1616
| ^^^^^ expected identifier, found keyword
@@ -52,17 +52,23 @@ LL | ($i: ident) => ($i)
5252
|
5353
::: $DIR/edition-keywords-2018-2015-parsing.rs:24:8
5454
|
55-
LL | if passes_ident!(async) == 1 {}
55+
LL | if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
5656
| -------------------- in this macro invocation
5757

58+
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
59+
--> $DIR/edition-keywords-2018-2015-parsing.rs:26:24
60+
|
61+
LL | if passes_tt!(async) == 1 {}
62+
| ^ expected one of `move`, `|`, or `||`
63+
5864
error[E0308]: mismatched types
59-
--> $DIR/edition-keywords-2018-2015-parsing.rs:29:33
65+
--> $DIR/edition-keywords-2018-2015-parsing.rs:31:33
6066
|
6167
LL | let _recovery_witness: () = 0;
6268
| -- ^ expected `()`, found integer
6369
| |
6470
| expected due to this
6571

66-
error: aborting due to 6 previous errors
72+
error: aborting due to 7 previous errors
6773

6874
For more information about this error, try `rustc --explain E0308`.

tests/ui/editions/edition-keywords-2018-2015.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn check_async() {
1818

1919
// if passes_ident!(async) == 1 {} // ERROR, reserved
2020
if passes_ident!(r#async) == 1 {} // OK
21+
// if passes_tt!(async) == 1 {} // ERROR, reserved
22+
if passes_tt!(r#async) == 1 {} // OK
2123
// one_async::async(); // ERROR, reserved
2224
one_async::r#async(); // OK
2325
// two_async::async(); // ERROR, reserved

0 commit comments

Comments
 (0)