Skip to content

Commit

Permalink
Rollup merge of rust-lang#61570 - varkor:infer-const-arg, r=eddyb
Browse files Browse the repository at this point in the history
Fix issues with const argument inference

Fixes rust-lang#60724.
Fixes rust-lang#61346.

r? @eddyb
  • Loading branch information
Centril authored Jun 10, 2019
2 parents 02d8876 + f11e6f7 commit 050262a
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 89 deletions.
21 changes: 11 additions & 10 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,7 @@ impl<'a> LoweringContext<'a> {
itctx: ImplTraitContext<'_>,
explicit_owner: Option<NodeId>,
) -> hir::PathSegment {
let (mut generic_args, infer_types) = if let Some(ref generic_args) = segment.args {
let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args {
let msg = "parenthesized type parameters may only be used with a `Fn` trait";
match **generic_args {
GenericArgs::AngleBracketed(ref data) => {
Expand Down Expand Up @@ -2230,17 +2230,17 @@ impl<'a> LoweringContext<'a> {
.collect();
if expected_lifetimes > 0 && param_mode == ParamMode::Explicit {
let anon_lt_suggestion = vec!["'_"; expected_lifetimes].join(", ");
let no_ty_args = generic_args.args.len() == expected_lifetimes;
let no_non_lt_args = generic_args.args.len() == expected_lifetimes;
let no_bindings = generic_args.bindings.is_empty();
let (incl_angl_brckt, insertion_span, suggestion) = if no_ty_args && no_bindings {
let (incl_angl_brckt, insertion_sp, suggestion) = if no_non_lt_args && no_bindings {
// If there are no (non-implicit) generic args or associated type
// bindings, our suggestion includes the angle brackets.
(true, path_span.shrink_to_hi(), format!("<{}>", anon_lt_suggestion))
} else {
// Otherwise (sorry, this is kind of gross) we need to infer the
// place to splice in the `'_, ` from the generics that do exist.
let first_generic_span = first_generic_span
.expect("already checked that type args or bindings exist");
.expect("already checked that non-lifetime args or bindings exist");
(false, first_generic_span.shrink_to_lo(), format!("{}, ", anon_lt_suggestion))
};
match self.anonymous_lifetime_mode {
Expand All @@ -2263,7 +2263,7 @@ impl<'a> LoweringContext<'a> {
expected_lifetimes,
path_span,
incl_angl_brckt,
insertion_span,
insertion_sp,
suggestion,
);
err.emit();
Expand All @@ -2280,7 +2280,7 @@ impl<'a> LoweringContext<'a> {
expected_lifetimes,
path_span,
incl_angl_brckt,
insertion_span,
insertion_sp,
suggestion,
)
);
Expand All @@ -2305,7 +2305,7 @@ impl<'a> LoweringContext<'a> {
Some(id),
Some(self.lower_res(res)),
generic_args,
infer_types,
infer_args,
)
}

Expand All @@ -2316,9 +2316,10 @@ impl<'a> LoweringContext<'a> {
mut itctx: ImplTraitContext<'_>,
) -> (hir::GenericArgs, bool) {
let &AngleBracketedArgs { ref args, ref constraints, .. } = data;
let has_types = args.iter().any(|arg| match arg {
let has_non_lt_args = args.iter().any(|arg| match arg {
ast::GenericArg::Lifetime(_) => false,
ast::GenericArg::Type(_) => true,
_ => false,
ast::GenericArg::Const(_) => true,
});
(
hir::GenericArgs {
Expand All @@ -2328,7 +2329,7 @@ impl<'a> LoweringContext<'a> {
.collect(),
parenthesized: false,
},
!has_types && param_mode == ParamMode::Optional
!has_non_lt_args && param_mode == ParamMode::Optional
)
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub struct PathSegment {
/// This only applies to expression and pattern paths, and
/// out of those only the segments with no type parameters
/// to begin with, e.g., `Vec::new` is `<Vec<..>>::new::<..>`.
pub infer_types: bool,
pub infer_args: bool,
}

impl PathSegment {
Expand All @@ -358,7 +358,7 @@ impl PathSegment {
ident,
hir_id: None,
res: None,
infer_types: true,
infer_args: true,
args: None,
}
}
Expand All @@ -368,13 +368,13 @@ impl PathSegment {
hir_id: Option<HirId>,
res: Option<Res>,
args: GenericArgs,
infer_types: bool,
infer_args: bool,
) -> Self {
PathSegment {
ident,
hir_id,
res,
infer_types,
infer_args,
args: if args.is_empty() {
None
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ impl<'a> State<'a> {

segment.with_generic_args(|generic_args| {
if !generic_args.args.is_empty() || !generic_args.bindings.is_empty() {
return self.print_generic_args(&generic_args, segment.infer_types, true);
return self.print_generic_args(&generic_args, segment.infer_args, true);
}
Ok(())
})?;
Expand Down Expand Up @@ -1561,7 +1561,7 @@ impl<'a> State<'a> {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types,
self.print_generic_args(generic_args, segment.infer_args,
colons_before_params)
})?;
}
Expand All @@ -1574,7 +1574,7 @@ impl<'a> State<'a> {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types, false)
self.print_generic_args(generic_args, segment.infer_args, false)
})?;
}
Ok(())
Expand Down Expand Up @@ -1602,7 +1602,7 @@ impl<'a> State<'a> {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args,
segment.infer_types,
segment.infer_args,
colons_before_params)
})?;
}
Expand All @@ -1614,7 +1614,7 @@ impl<'a> State<'a> {
self.print_ident(item_segment.ident)?;
item_segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args,
item_segment.infer_types,
item_segment.infer_args,
colons_before_params)
})
}
Expand All @@ -1626,7 +1626,7 @@ impl<'a> State<'a> {
self.print_ident(item_segment.ident)?;
item_segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args,
item_segment.infer_types,
item_segment.infer_args,
colons_before_params)
})
}
Expand All @@ -1635,7 +1635,7 @@ impl<'a> State<'a> {

fn print_generic_args(&mut self,
generic_args: &hir::GenericArgs,
infer_types: bool,
infer_args: bool,
colons_before_params: bool)
-> io::Result<()> {
if generic_args.parenthesized {
Expand Down Expand Up @@ -1681,7 +1681,7 @@ impl<'a> State<'a> {

// FIXME(eddyb): this would leak into error messages (e.g.,
// "non-exhaustive patterns: `Some::<..>(_)` not covered").
if infer_types && false {
if infer_args && false {
start_or_comma(self)?;
self.s.word("..")?;
}
Expand Down
Loading

0 comments on commit 050262a

Please sign in to comment.