Skip to content

Commit dcd3cf7

Browse files
committed
Do not modify mutability of simple bindings.
This commit removes the modification of the mutability of simple bindings. While the mutability isn't used, it is important that it is kept so that the input to procedural macros matches what the user wrote. This commit also modifies the span of the binding mode so that it is considered a compiler desugaring and won't be linted against for being unused..
1 parent e57c7b8 commit dcd3cf7

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

src/librustc/hir/lowering.rs

+11-33
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use errors::Applicability;
5050
use rustc_data_structures::fx::FxHashSet;
5151
use rustc_data_structures::indexed_vec::IndexVec;
5252
use rustc_data_structures::thin_vec::ThinVec;
53-
use rustc_data_structures::sync::Lrc;
5453

5554
use std::collections::{BTreeSet, BTreeMap};
5655
use std::mem;
@@ -59,10 +58,10 @@ use syntax::attr;
5958
use syntax::ast;
6059
use syntax::ast::*;
6160
use syntax::errors;
62-
use syntax::ext::hygiene::{Mark, SyntaxContext};
61+
use syntax::ext::hygiene::Mark;
6362
use syntax::print::pprust;
6463
use syntax::ptr::P;
65-
use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned};
64+
use syntax::source_map::{respan, CompilerDesugaringKind, Spanned};
6665
use syntax::std_inject;
6766
use syntax::symbol::{keywords, Symbol};
6867
use syntax::tokenstream::{TokenStream, TokenTree};
@@ -855,27 +854,6 @@ impl<'a> LoweringContext<'a> {
855854
Ident::with_empty_ctxt(Symbol::gensym(s))
856855
}
857856

858-
/// Reuses the span but adds information like the kind of the desugaring and features that are
859-
/// allowed inside this span.
860-
fn mark_span_with_reason(
861-
&self,
862-
reason: CompilerDesugaringKind,
863-
span: Span,
864-
allow_internal_unstable: Option<Lrc<[Symbol]>>,
865-
) -> Span {
866-
let mark = Mark::fresh(Mark::root());
867-
mark.set_expn_info(source_map::ExpnInfo {
868-
call_site: span,
869-
def_site: Some(span),
870-
format: source_map::CompilerDesugaring(reason),
871-
allow_internal_unstable,
872-
allow_internal_unsafe: false,
873-
local_inner_macros: false,
874-
edition: source_map::hygiene::default_edition(),
875-
});
876-
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
877-
}
878-
879857
fn with_anonymous_lifetime_mode<R>(
880858
&mut self,
881859
anonymous_lifetime_mode: AnonymousLifetimeMode,
@@ -1164,7 +1142,7 @@ impl<'a> LoweringContext<'a> {
11641142
attrs: ThinVec::new(),
11651143
};
11661144

1167-
let unstable_span = self.mark_span_with_reason(
1145+
let unstable_span = self.sess.source_map().mark_span_with_reason(
11681146
CompilerDesugaringKind::Async,
11691147
span,
11701148
Some(vec![
@@ -1571,7 +1549,7 @@ impl<'a> LoweringContext<'a> {
15711549
// desugaring that explicitly states that we don't want to track that.
15721550
// Not tracking it makes lints in rustc and clippy very fragile as
15731551
// frequently opened issues show.
1574-
let exist_ty_span = self.mark_span_with_reason(
1552+
let exist_ty_span = self.sess.source_map().mark_span_with_reason(
15751553
CompilerDesugaringKind::ExistentialReturnType,
15761554
span,
15771555
None,
@@ -2446,7 +2424,7 @@ impl<'a> LoweringContext<'a> {
24462424
) -> hir::FunctionRetTy {
24472425
let span = output.span();
24482426

2449-
let exist_ty_span = self.mark_span_with_reason(
2427+
let exist_ty_span = self.sess.source_map().mark_span_with_reason(
24502428
CompilerDesugaringKind::Async,
24512429
span,
24522430
None,
@@ -4182,7 +4160,7 @@ impl<'a> LoweringContext<'a> {
41824160
}),
41834161
ExprKind::TryBlock(ref body) => {
41844162
self.with_catch_scope(body.id, |this| {
4185-
let unstable_span = this.mark_span_with_reason(
4163+
let unstable_span = this.sess.source_map().mark_span_with_reason(
41864164
CompilerDesugaringKind::TryBlock,
41874165
body.span,
41884166
Some(vec![
@@ -4615,7 +4593,7 @@ impl<'a> LoweringContext<'a> {
46154593
// expand <head>
46164594
let mut head = self.lower_expr(head);
46174595
let head_sp = head.span;
4618-
let desugared_span = self.mark_span_with_reason(
4596+
let desugared_span = self.sess.source_map().mark_span_with_reason(
46194597
CompilerDesugaringKind::ForLoop,
46204598
head_sp,
46214599
None,
@@ -4776,15 +4754,15 @@ impl<'a> LoweringContext<'a> {
47764754
// return Try::from_error(From::from(err)),
47774755
// }
47784756

4779-
let unstable_span = self.mark_span_with_reason(
4757+
let unstable_span = self.sess.source_map().mark_span_with_reason(
47804758
CompilerDesugaringKind::QuestionMark,
47814759
e.span,
47824760
Some(vec![
47834761
Symbol::intern("try_trait")
47844762
].into()),
47854763
);
47864764
let try_span = self.sess.source_map().end_point(e.span);
4787-
let try_span = self.mark_span_with_reason(
4765+
let try_span = self.sess.source_map().mark_span_with_reason(
47884766
CompilerDesugaringKind::QuestionMark,
47894767
try_span,
47904768
Some(vec![
@@ -5569,12 +5547,12 @@ impl<'a> LoweringContext<'a> {
55695547
);
55705548
self.sess.abort_if_errors();
55715549
}
5572-
let span = self.mark_span_with_reason(
5550+
let span = self.sess.source_map().mark_span_with_reason(
55735551
CompilerDesugaringKind::Await,
55745552
await_span,
55755553
None,
55765554
);
5577-
let gen_future_span = self.mark_span_with_reason(
5555+
let gen_future_span = self.sess.source_map().mark_span_with_reason(
55785556
CompilerDesugaringKind::Await,
55795557
await_span,
55805558
Some(vec![Symbol::intern("gen_future")].into()),

src/libsyntax/parse/parser.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ use crate::symbol::{Symbol, keywords};
5050

5151
use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError};
5252
use rustc_target::spec::abi::{self, Abi};
53-
use syntax_pos::{Span, MultiSpan, BytePos, FileName};
53+
use syntax_pos::{
54+
Span, MultiSpan, BytePos, FileName,
55+
hygiene::CompilerDesugaringKind,
56+
};
5457
use log::{debug, trace};
5558

5659
use std::borrow::Cow;
@@ -8741,6 +8744,15 @@ impl<'a> Parser<'a> {
87418744
// statement.
87428745
let (binding_mode, ident, is_simple_pattern) = match input.pat.node {
87438746
PatKind::Ident(binding_mode @ BindingMode::ByValue(_), ident, _) => {
8747+
// Simple patterns like this don't have a generated argument, but they are
8748+
// moved into the closure with a statement, so any `mut` bindings on the
8749+
// argument will be unused. This binding mode can't be removed, because
8750+
// this would affect the input to procedural macros, but they can have
8751+
// their span marked as being the result of a compiler desugaring so
8752+
// that they aren't linted against.
8753+
input.pat.span = self.sess.source_map().mark_span_with_reason(
8754+
CompilerDesugaringKind::Async, span, None);
8755+
87448756
(binding_mode, ident, true)
87458757
}
87468758
_ => (BindingMode::ByValue(Mutability::Mutable), ident, false),
@@ -8810,15 +8822,6 @@ impl<'a> Parser<'a> {
88108822
})
88118823
};
88128824

8813-
// Remove mutability from arguments. If this is not a simple pattern,
8814-
// those arguments are replaced by `__argN`, so there is no need to do this.
8815-
if let PatKind::Ident(BindingMode::ByValue(mutability @ Mutability::Mutable), ..) =
8816-
&mut input.pat.node
8817-
{
8818-
assert!(is_simple_pattern);
8819-
*mutability = Mutability::Immutable;
8820-
}
8821-
88228825
let move_stmt = Stmt { id, node: StmtKind::Local(P(move_local)), span };
88238826
arguments.push(AsyncArgument { ident, arg, pat_stmt, move_stmt });
88248827
}

src/libsyntax/source_map.rs

+21
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,27 @@ impl SourceMap {
930930

931931
None
932932
}
933+
934+
/// Reuses the span but adds information like the kind of the desugaring and features that are
935+
/// allowed inside this span.
936+
pub fn mark_span_with_reason(
937+
&self,
938+
reason: hygiene::CompilerDesugaringKind,
939+
span: Span,
940+
allow_internal_unstable: Option<Lrc<[symbol::Symbol]>>,
941+
) -> Span {
942+
let mark = Mark::fresh(Mark::root());
943+
mark.set_expn_info(ExpnInfo {
944+
call_site: span,
945+
def_site: Some(span),
946+
format: CompilerDesugaring(reason),
947+
allow_internal_unstable,
948+
allow_internal_unsafe: false,
949+
local_inner_macros: false,
950+
edition: hygiene::default_edition(),
951+
});
952+
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
953+
}
933954
}
934955

935956
impl SourceMapper for SourceMap {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
async fn f(x: u8) { }
1+
async fn f(mut x: u8) { }

0 commit comments

Comments
 (0)