Commit Graph

55 Commits

Author SHA1 Message Date
Slava Pestov
10a2ddb95e RequirementMachine: Fix MaxConcreteNesting check to take initial rules into account
Fixes rdar://123357717.
2024-03-06 21:42:49 -05:00
Slava Pestov
1b99dd2451 RequirementMachine: Tweak RewriteSystem::recordConflict() heuristic again 2024-02-29 18:13:28 -05:00
Slava Pestov
70c9f8a47e RequirementMachine: Leave behind conflicting requirements in the minimized signature
Requirement lowering only expects that it won't see two requirements
of the same kind (except for conformance requirements). So only mark
those as conflicting.

This addresses a crash-on-invalid and improves diagnostics for
move-only generics, because a conflict won't drop the copyability
of a generic parameter and expose a move-only-naive user to
confusing error messages.

Fixes #61031.
Fixes #63997.
Fixes rdar://problem/111991454.
2024-02-15 14:32:31 -05:00
Nate Chandler
32156b5a99 [RequirementMachine] Used loc in sig reqs.
`InferredGenericSignatureRequest` creates `StructuralRequirement`s for
the requirements of the generic signature that is passed to it (if one
is).

Previously, it used invalid `SourceLoc`s for these requirements.  The
result was that when errors that were emitted as a result of those
`StructuralRequirement`s (during concrete type contraction), they would
also have invalid `SourceLoc`s.  The effect was that those errors were
ignored during `diagnoseRequirementErrors`.

Here, use the available loc for those requirements.

rdar://108963047
2023-05-17 15:16:23 -07:00
Nate Chandler
55d2ce7545 [Test] Skip redundancy warnings in attr_specialize 2023-05-17 11:26:33 -07:00
Slava Pestov
9339443e79 RequirementMachine: Diagnose recursive requirements
Note the test cases in abstract_type_witnesses used to pass but are now
rejected. This is fine, because doing anything more complicated used to
crash, and the GSB would crash or misbehave with these examples.
2022-08-11 14:12:06 -04:00
Slava Pestov
dac8d666ee Stop passing -requirement-machine-{abstract,inferred,protocol}-signatures flags in tests
These flags are now no-ops.
2022-05-10 12:56:17 -04:00
Slava Pestov
f39372b33d RequirementMachine: Turn off redundant requirement warnings by default and add -warn-redundant-requirements frontend flag 2022-05-10 01:49:56 -04:00
Slava Pestov
d385b73cd6 RequirementMachine: Plumb source locations through concrete contraction 2022-04-01 22:33:03 -04:00
Slava Pestov
305a1e42b6 RequirementMachine: Update some tests to pass with -requirement-machine-inferred-signatures=verify 2022-03-14 12:33:18 -04:00
Slava Pestov
7b01ff60f0 RequirementMachine: Simplify RewriteSystem::processConflicts()
Remove the logic which incorrectly attempted to simulate the
GenericSignatureBuilder's minimization behavior in the presence
of conflicts, since it doesn't matter anymore.
2022-03-14 12:33:18 -04:00
Karoy Lorentey
47956908b7 [Concurrency] SwiftStdlib 5.5 ⟹ SwiftStdlib 5.1 (usages)
The concurrency runtime now deploys back to macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, which corresponds to the 5.1 release of the stdlib.

Adjust macro usages accordingly.
2021-10-28 14:36:36 -07:00
Arnold Schwaighofer
8840ea6b5b Add support for parsing an availability argument in @_specialize 2021-10-05 14:46:17 -07:00
Slava Pestov
ad89fe5179 GSB: Diagnose redundant requirements on the rebuilt signature
We rebuild a generic signature after dropping conformance requirements
made redundant by a superclass or concrete type requirement.

When rebuilding the signature, preserve the source locations of the
original requirements, and only perform diagnostics on the rebuilt
signature.

This fixes an issue where we would emit a redundant requirement
warning even though the requirement in question was not actually
redundant.

This also avoids some unnecessary work. Most of the code in
finalize() does not need to be run twice, once before and once after
rebuilding the signature. Now we only run it after rebuilding the
signature.

Note that this regresses diagnostics in one narrow case where we
would previously diagnose a conflicting concrete type requirement.
This will be fixed once concrete type diagnostics are moved over
to use the new ExplicitRequirement infrastructure, just like all
other kinds already do.

Fixes rdar://problem/77462797.
2021-05-25 21:50:54 -04:00
Slava Pestov
f190e51f8d GSB: Diagnose redundant superclass requirements using the redundant requirement graph 2021-04-03 22:33:14 -04:00
Slava Pestov
299ff02684 GSB: Diagnose redundant layout requirements using the redundant requirement graph 2021-03-27 00:35:19 -04:00
Slava Pestov
568d99ffd1 Sema: Clean up @_specialize diagnostics
Instead of looking at the requirements before passing them off to
the GSB, let's look at the difference between the final signature
and the original signature.

This makes the checks more accurate, for example we want to
detect that 'E' was specialized in the below:

  @_specialize(where S == Set<String>)
  public func takesSequenceAndElement<S, E>(_: S, _: E)
    where S : Sequence, E == S.Element {}
2021-03-17 23:07:11 -04:00
Arnold Schwaighofer
084db0d38f Revert "Merge pull request #34848 from aschwaighofer/make_prespecialization_experimental"
This reverts commit 3aec862e62, reversing
changes made to 158427bd5b.
2021-02-12 10:12:01 -08:00
Arnold Schwaighofer
8346bf7e90 Pre-specialization: This is an experimental feature
Only enable if explicitly required.
2020-11-20 09:13:16 -08:00
Arnold Schwaighofer
b994bf3191 Add support for _specialize(exported: true, ...)
This attribute allows to define a pre-specialized entry point of a
generic function in a library.

The following definition provides a pre-specialized entry point for
`genericFunc(_:)` for the parameter type `Int` that clients of the
library can call.

```
@_specialize(exported: true, where T == Int)
public func genericFunc<T>(_ t: T) { ... }
```

Pre-specializations of internal `@inlinable` functions are allowed.

```
@usableFromInline
internal struct GenericThing<T> {
  @_specialize(exported: true, where T == Int)
  @inlinable
  internal func genericMethod(_ t: T) {
  }
}
```

There is syntax to pre-specialize a method from a different module.

```
import ModuleDefiningGenericFunc

@_specialize(exported: true, target: genericFunc(_:), where T == Double)
func prespecialize_genericFunc(_ t: T) { fatalError("dont call") }

```

Specially marked extensions allow for pre-specialization of internal
methods accross module boundries (respecting `@inlinable` and
`@usableFromInline`).

```
import ModuleDefiningGenericThing
public struct Something {}

@_specializeExtension
extension GenericThing {
  @_specialize(exported: true, target: genericMethod(_:), where T == Something)
  func prespecialize_genericMethod(_ t: T) { fatalError("dont call") }
}
```

rdar://64993425
2020-10-12 09:19:29 -07:00
Varun Gandhi
a1716fe2a6 [Diagnostics] Update compiler diagnostics to use less jargon. (#31315)
Fixes rdar://problem/62375243.
2020-04-28 14:11:39 -07:00
Xi Ge
435d13496e AST: making export: true in @_specialized attribute a no-operation
The client code doesn't actually call into these specialized functions even
though they have public linkage. This could lead to TBD verification failure
shown in rdar://44777994.

This patch also warns users' codebase when `export: true` is specified.
2020-04-10 16:52:22 -07:00
fischertony
e78a42b06f [SR-8239][Parse] Fix same-type constraint commutativity 2018-09-11 05:18:44 +03:00
Doug Gregor
d1ea1813c9 [Type checker] Reimplement validation of @_specialize attribute.
Reimplement the validation of the @_specialize attribute to make use of
RequirementRequest::visitInherited() rather than performing ad hoc
validation.
2018-08-23 23:41:09 -07:00
Doug Gregor
c94d1cd31a [Type checker] The term "layout constraint" is confusing; don't use it.
Within the compiler, we use the term "layout constraint" for any
constraint that affects the layout of a type parameter that has that
constraint. However, the only user-visible constraint is "AnyObject",
and calling that a layout constraint is confusing. Drop the term
"layout" from diagnostics.

Fixes rdar://problem/35295372.
2017-11-02 12:03:09 -07:00
Doug Gregor
770912e87d Fix test case 2017-10-10 23:06:52 -07:00
Slava Pestov
1e3d88e919 Update a test 2017-09-05 22:13:18 -07:00
Slava Pestov
eb46696baa AST: Fix bogus diagnostic with bad conformance requirements in generic signature
Fixes <rdar://problem/33604221>.
2017-07-31 14:19:19 -07:00
Alex Hoppen
f35f29d9cf [Diag] Change function diagnostics to take a DeclName parameter
This provides richer error messages that include the function's
parameters
2017-07-01 13:37:08 +02:00
Doug Gregor
c47aea7150 [GSB] Cope with typealiases within protocol hierarchies.
When we see two type(aliase)s with the same name in a protocol
hierarchy, make them equal with an implied same-type requirement. This
detects inconstencies in typealiases across different protocols, and
eliminates the need for ad hoc consistency checking. This is a step
toward simplifying away the need for direct-diagnosis operations
involving concrete type mismatches.

While here, warn when we see an associated type with the same as a
typealias from an inherited protocol; in this case, the associated
type is basically useless, because it's going to be equivalent to the
typealias.
2017-06-23 17:25:45 -07:00
Doug Gregor
c522bb5239 [GSB] Separate out "unresolved" and "direct" type requirement handling.
As we've done with layout requirements, introduce a new entry point
(addTypeRequirement) that handles unresolved type requirements of the
form `T: U`, resolves the types, and then can

1. Diagnose any immediate problems with the types,
2. Delay the type requirement if one of the types cannot be resolved,
or
3. Break it into one or more "direct" requirements.

This allows us to clean up and centralize a bunch of checking that was
scattered/duplicated across the GSB and type checker.
2017-04-07 16:53:11 -07:00
Roman Levenstein
2f91ee197d Add a syntax to express the new _Class and _Native class layout constraints
The syntax is "T: _Class" and "T: _NativeClass".
2017-03-24 16:32:23 -07:00
Roman Levenstein
e3bc23cc09 Merge pull request #8253 from swiftix/wip-gsb-layout-constrains-fixes
[GSB] Improve handling of layout constraints
2017-03-22 17:36:05 -07:00
Roman Levenstein
e1403c6dc2 [GSB] Improve handling of layout constraints
This PR addresses TODOs from #8241.

- It supports merging for layout constraints, e.g., if both a _Trivial constraint and a _Trivial(64) constraint appear on a type parameter, we keep only _Trivial(64) as a more specific layout constraint. We do a similar thing for ref-counted/native-ref-counted. The overall idea is to keep the more specific of two compatible layout constraints.
- The presence of a superclass constraint implies a layout constraint, e.g., a superclass constraint implies _Class or _NativeClass
2017-03-22 16:39:02 -07:00
Doug Gregor
ddc2775530 [GSB] Diagnose redundant same-type constraints.
Diagnose redundant same-type constraints using most of the same
machinery for diagnosing other redundant constraints. However,
same-type constraints are particularly interesting because
redundancies can be spelled in a number of different ways. Address
this using the connected components of the subgraph involving only
derived requirements (which is already used for the minimized generic
signature). Then, separate all of the non-derived requirements into
the intracomponent requirements and intercomponent requirements:

* All of the intracomponent requirements are redundant by definition,
  because the components are defined by derived constraints.

* For the intercomponent requirements, form a spanning tree among the
  various components and diagnose as redundant any edges that do not
  extend the spanning tree.
2017-03-21 23:02:04 -07:00
Doug Gregor
5aa51e9532 [GSB] Keep track of all layout constraints.
As we've done with all of the other kinds of constraints, keep track
of all of the layout constraints on the equivalence class. Use the
normal mechanism to diagnose conflicts between different layout
constraints, warn about duplicate layout constraints, etc.
2017-03-21 06:59:40 -07:00
Doug Gregor
6142329a1e [Type checker] Finalize the GenericSignatureBuilder for @_specialize.
To diagnose issues with the where clause in an @_specialize attribute,
the GenericSignatureBuilder needs to be finalized first.
2017-02-28 20:50:21 -08:00
Doug Gregor
d697c2fdcb [GenericSig Builder] Diagnose redundant same-typeo-t-concrete constraints.
Diagnose when a same-type constraint (to a concrete type) is made
redundant by another same-type constraint. Slightly improve the
diagnostic that handles collisions between two same-type constraints.
2017-02-24 10:44:30 -08:00
Doug Gregor
b412961003 [AST] Maintain type sugar in TypeMatcher. 2017-02-09 15:03:34 -08:00
Doug Gregor
8756772485 [Archetype builder] Allow one to directly express constraints X<T> == X<U>.
All of the implementation work to make this possible was completed in
prior commits, so loosen the restriction from "one side must be a type
parameter" or "one side must contain a type parameter".
2017-02-09 14:13:11 -08:00
Doug Gregor
e93ae1d9a8 [Archetype builder] Perform structural matching of same-type constraints.
Rather than using "isEqual" to match same-type constraints among
concrete types, use a full type matched to recursively decompose the
structure. This allows us to support same-type constraints that end up
being of the form X<T> == X<U>, where T and U are type parameters of
some sort.

Fixes rdar://problem/29333056.
2017-02-09 14:13:10 -08:00
Doug Gregor
0c76a9d828 [Archetype builder] Clean up PotentialArchetype a bit.
Clean up the representation of PotentialArchetype in a few small ways:

* Eliminate the GenericTypeParamType* at the root, and instead just
  store a GenericParamKey. That makes the potential archetypes
  independent of a particular set of generic parameters.

* Give potential archetypes a link back to their owning
  ArchetypeBuilder, so we can get contextual information (etc.) when
  needed. We can remove the "builder" arguments as a separate step.

Also, collapse getName()/getDebugName()/getFullName() into
getNestedName() and getDebugName(). Generic parameters don't have
"names" per se, so they should only show up in debug dumps.

In support of the former, clean up some of the diagnostics emitted by
the archetype builder that were using 'Identifier' or 'StringRef'
where they should have been using a 'Type' (i.e., the type behind the
dependent archetype).
2017-02-07 11:15:11 -08:00
Roman Levenstein
88d6e5c43b Make diagnostics for @_specialize look similar to other attributes 2017-01-18 16:43:42 -08:00
Roman Levenstein
de07095e01 Update and enhance tests to use the new @_specialize syntax and features 2017-01-18 16:43:42 -08:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Rintaro Ishizaki
1b51bc1d6b [Sema] Check existence of GenericSignature before getting GenericParamList from it
Fixes 2 compiler crasher.
2016-11-14 22:01:53 +09:00
Slava Pestov
a1eef126ba AST: Don't print "aka <<desugared type>>" for generic function types
This was causing us to emit diagnostics talking about τ_m_n, which is
not helpful.

Now that generic function types print sanely, print them in a few
places where we were previously printing PolymorphicFunctionTypes.
2016-09-15 21:47:57 -07:00
Argyrios Kyrtzidis
69918a966d [ASTPrinter] Fix printing of nested typealias types and make it consistent with printing of nominal types.
This fixes several issues:
- By default parent types of alias types are not printed which results in
	- Erroneous fixits, for example when casting to 'Notification.Name' from a string, which ends up adding erroneous cast
	  as "Name(rawValue: ...)"
	- Hard to understand types in code-completion results and diagnostics
- When printing with 'fully-qualified' option typealias types are printed erroneously like this "<PARENT>.Type.<TYPEALIAS>"

The change make typealias printing same as nominal types and addresses the above.
2016-08-11 12:15:15 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00