Skip to content

Rollup of 5 pull requests #66236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,10 @@ pub(crate) mod builtin {
}

/// Inline assembly.
///
/// Read the [unstable book] for the usage.
///
/// [unstable book]: ../unstable-book/library-features/asm.html
#[unstable(feature = "asm", issue = "29722",
reason = "inline assembly is not stable enough for use and is subject to change")]
#[rustc_builtin_macro]
Expand Down
24 changes: 17 additions & 7 deletions src/libfmt_macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl InnerOffset {

/// A piece is a portion of the format string which represents the next part
/// to emit. These are emitted as a stream by the `Parser` class.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Piece<'a> {
/// A literal string which should directly be emitted
String(&'a str),
Expand All @@ -45,7 +45,7 @@ pub enum Piece<'a> {
}

/// Representation of an argument specification.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Argument<'a> {
/// Where to find this argument
pub position: Position,
Expand All @@ -54,7 +54,7 @@ pub struct Argument<'a> {
}

/// Specification for the formatting of an argument in the format string.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FormatSpec<'a> {
/// Optionally specified character to fill alignment with.
pub fill: Option<char>,
Expand All @@ -74,10 +74,12 @@ pub struct FormatSpec<'a> {
/// this argument, this can be empty or any number of characters, although
/// it is required to be one word.
pub ty: &'a str,
/// The span of the descriptor string (for diagnostics).
pub ty_span: Option<InnerSpan>,
}

/// Enum describing where an argument for a format can be located.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Position {
/// The argument is implied to be located at an index
ArgumentImplicitlyIs(usize),
Expand All @@ -97,7 +99,7 @@ impl Position {
}

/// Enum of alignments which are supported.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Alignment {
/// The value will be aligned to the left.
AlignLeft,
Expand All @@ -111,7 +113,7 @@ pub enum Alignment {

/// Various flags which can be applied to format strings. The meaning of these
/// flags is defined by the formatters themselves.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Flag {
/// A `+` will be used to denote positive numbers.
FlagSignPlus,
Expand All @@ -131,7 +133,7 @@ pub enum Flag {

/// A count is used for the precision and width parameters of an integer, and
/// can reference either an argument or a literal integer.
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Count {
/// The count is specified explicitly.
CountIs(usize),
Expand Down Expand Up @@ -475,6 +477,7 @@ impl<'a> Parser<'a> {
width: CountImplied,
width_span: None,
ty: &self.input[..0],
ty_span: None,
};
if !self.consume(':') {
return spec;
Expand Down Expand Up @@ -548,6 +551,7 @@ impl<'a> Parser<'a> {
spec.precision_span = sp;
}
}
let ty_span_start = self.cur.peek().map(|(pos, _)| *pos);
// Optional radix followed by the actual format specifier
if self.consume('x') {
if self.consume('?') {
Expand All @@ -567,6 +571,12 @@ impl<'a> Parser<'a> {
spec.ty = "?";
} else {
spec.ty = self.word();
let ty_span_end = self.cur.peek().map(|(pos, _)| *pos);
if !spec.ty.is_empty() {
spec.ty_span = ty_span_start
.and_then(|s| ty_span_end.map(|e| (s, e)))
.map(|(start, end)| self.to_span_index(start).to(self.to_span_index(end)));
}
}
spec
}
Expand Down
43 changes: 28 additions & 15 deletions src/libfmt_macros/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

fn same(fmt: &'static str, p: &[Piece<'static>]) {
let parser = Parser::new(fmt, None, vec![], false);
assert!(parser.collect::<Vec<Piece<'static>>>() == p);
assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p);
}

fn fmtdflt() -> FormatSpec<'static> {
Expand All @@ -15,6 +15,7 @@ fn fmtdflt() -> FormatSpec<'static> {
precision_span: None,
width_span: None,
ty: "",
ty_span: None,
};
}

Expand Down Expand Up @@ -82,7 +83,7 @@ fn format_position_nothing_else() {
#[test]
fn format_type() {
same(
"{3:a}",
"{3:x}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
Expand All @@ -93,7 +94,8 @@ fn format_type() {
width: CountImplied,
precision_span: None,
width_span: None,
ty: "a",
ty: "x",
ty_span: None,
},
})]);
}
Expand All @@ -112,6 +114,7 @@ fn format_align_fill() {
precision_span: None,
width_span: None,
ty: "",
ty_span: None,
},
})]);
same(
Expand All @@ -127,6 +130,7 @@ fn format_align_fill() {
precision_span: None,
width_span: None,
ty: "",
ty_span: None,
},
})]);
same(
Expand All @@ -142,6 +146,7 @@ fn format_align_fill() {
precision_span: None,
width_span: None,
ty: "abcd",
ty_span: Some(InnerSpan::new(6, 10)),
},
})]);
}
Expand All @@ -150,7 +155,7 @@ fn format_counts() {
use syntax_pos::{GLOBALS, Globals, edition};
GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
same(
"{:10s}",
"{:10x}",
&[NextArgument(Argument {
position: ArgumentImplicitlyIs(0),
format: FormatSpec {
Expand All @@ -161,11 +166,12 @@ fn format_counts() {
width: CountIs(10),
precision_span: None,
width_span: None,
ty: "s",
ty: "x",
ty_span: None,
},
})]);
same(
"{:10$.10s}",
"{:10$.10x}",
&[NextArgument(Argument {
position: ArgumentImplicitlyIs(0),
format: FormatSpec {
Expand All @@ -176,11 +182,12 @@ fn format_counts() {
width: CountIsParam(10),
precision_span: None,
width_span: Some(InnerSpan::new(3, 6)),
ty: "s",
ty: "x",
ty_span: None,
},
})]);
same(
"{:.*s}",
"{:.*x}",
&[NextArgument(Argument {
position: ArgumentImplicitlyIs(1),
format: FormatSpec {
Expand All @@ -191,11 +198,12 @@ fn format_counts() {
width: CountImplied,
precision_span: Some(InnerSpan::new(3, 5)),
width_span: None,
ty: "s",
ty: "x",
ty_span: None,
},
})]);
same(
"{:.10$s}",
"{:.10$x}",
&[NextArgument(Argument {
position: ArgumentImplicitlyIs(0),
format: FormatSpec {
Expand All @@ -206,11 +214,12 @@ fn format_counts() {
width: CountImplied,
precision_span: Some(InnerSpan::new(3, 7)),
width_span: None,
ty: "s",
ty: "x",
ty_span: None,
},
})]);
same(
"{:a$.b$s}",
"{:a$.b$?}",
&[NextArgument(Argument {
position: ArgumentImplicitlyIs(0),
format: FormatSpec {
Expand All @@ -221,7 +230,8 @@ fn format_counts() {
width: CountIsName(Symbol::intern("a")),
precision_span: None,
width_span: None,
ty: "s",
ty: "?",
ty_span: None,
},
})]);
});
Expand All @@ -241,6 +251,7 @@ fn format_flags() {
precision_span: None,
width_span: None,
ty: "",
ty_span: None,
},
})]);
same(
Expand All @@ -256,13 +267,14 @@ fn format_flags() {
precision_span: None,
width_span: None,
ty: "",
ty_span: None,
},
})]);
}
#[test]
fn format_mixture() {
same(
"abcd {3:a} efg",
"abcd {3:x} efg",
&[
String("abcd "),
NextArgument(Argument {
Expand All @@ -275,7 +287,8 @@ fn format_mixture() {
width: CountImplied,
precision_span: None,
width_span: None,
ty: "a",
ty: "x",
ty_span: None,
},
}),
String(" efg"),
Expand Down
14 changes: 12 additions & 2 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,16 @@ impl<'a> LoweringContext<'a> {
impl_trait_return_allow: bool,
make_ret_async: Option<NodeId>,
) -> P<hir::FnDecl> {
debug!("lower_fn_decl(\
fn_decl: {:?}, \
in_band_ty_params: {:?}, \
impl_trait_return_allow: {}, \
make_ret_async: {:?})",
decl,
in_band_ty_params,
impl_trait_return_allow,
make_ret_async,
);
let lt_mode = if make_ret_async.is_some() {
// In `async fn`, argument-position elided lifetimes
// must be transformed into fresh generic parameters so that
Expand Down Expand Up @@ -2412,7 +2422,7 @@ impl<'a> LoweringContext<'a> {

hir::FunctionRetTy::Return(P(hir::Ty {
kind: opaque_ty_ref,
span,
span: opaque_ty_span,
hir_id: self.next_id(),
}))
}
Expand Down Expand Up @@ -2522,7 +2532,7 @@ impl<'a> LoweringContext<'a> {
hir::Lifetime {
hir_id: self.lower_node_id(id),
span,
name: name,
name,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
///
/// It will later be extended to trait objects.
pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
let (span, sub, sup) = self.get_regions();
let (span, sub, sup) = self.regions();

// Determine whether the sub and sup consist of both anonymous (elided) regions.
let anon_reg_sup = self.tcx().is_suitable_region(sup)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
.or_else(|| self.try_report_impl_not_conforming_to_trait())
}

pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
pub fn regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
match (&self.error, self.regions) {
(Some(ConcreteFailure(origin, sub, sup)), None) => (origin.span(), sub, sup),
(Some(SubSupConflict(_, _, origin, sub, _, sup)), None) => (origin.span(), sub, sup),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// When given a `ConcreteFailure` for a function with parameters containing a named region and
/// an anonymous region, emit an descriptive diagnostic error.
pub(super) fn try_report_named_anon_conflict(&self) -> Option<DiagnosticBuilder<'a>> {
let (span, sub, sup) = self.get_regions();
let (span, sub, sup) = self.regions();

debug!(
"try_report_named_anon_conflict(sub={:?}, sup={:?}, error={:?})",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
) = error.clone()
{
let anon_reg_sup = self.tcx().is_suitable_region(sup_r)?;
let return_ty = self.tcx().return_type_impl_trait(anon_reg_sup.def_id);
if sub_r == &RegionKind::ReStatic &&
self.tcx().return_type_impl_trait(anon_reg_sup.def_id).is_some()
return_ty.is_some()
{
let sp = var_origin.span();
let return_sp = sub_origin.span();
Expand Down Expand Up @@ -52,17 +53,23 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}) => name.to_string(),
_ => "'_".to_owned(),
};
if let Ok(snippet) = self.tcx().sess.source_map().span_to_snippet(return_sp) {
err.span_suggestion(
return_sp,
&format!(
"you can add a constraint to the return type to make it last \
let fn_return_span = return_ty.unwrap().1;
if let Ok(snippet) =
self.tcx().sess.source_map().span_to_snippet(fn_return_span) {
// only apply this suggestion onto functions with
// explicit non-desugar'able return.
if fn_return_span.desugaring_kind().is_none() {
err.span_suggestion(
fn_return_span,
&format!(
"you can add a constraint to the return type to make it last \
less than `'static` and match {}",
lifetime,
),
format!("{} + {}", snippet, lifetime_name),
Applicability::Unspecified,
);
lifetime,
),
format!("{} + {}", snippet, lifetime_name),
Applicability::Unspecified,
);
}
}
err.emit();
return Some(ErrorReported);
Expand Down
Loading