A naive digital square wave will absolutely generate aliasing, there is an entire area of audio DSP research aimed to generate alias-free waveforms (another poster mentioned BLEP which is a popular technique).
You can trivially see this by looking at a spectrogram of a naively synthesized waveform- all kinds of junk will appear that is also clearly audible.
This is because the "naive" approach to e.g. a digital square wave is to sample a logically continuous function defined as:
if (less than halfway through the cycle period) -1
else 1
Firstly, this is almost always going to be out of tune because the transitions from -1 to 1 and 1 to -1 necessarily fall across a digital sample boundary.
For any frequency not evenly dividing the sample rate the transition will be slightly early or late.
Secondly, the sharp rising edge of the square wave, from a "sum of sines" perspective, requires sinusoid components that are not part of a conventional square wave to represent them digitally.
If you were to digitally sample an analog square wave through an ADC it would look quite different from the result of this function.
The naive digital sawtooth wave suffers from similar issues due to its discontinuity.
An ideal square wave is not a signal that is band-limited to the audio range, so attempting to sample it directly (the naive approach) doesn't work. Instead, you want to sample a band-limited approximation of the square wave (which is what BLEP, etc. do).
If you're synthesizing, the naive approach doesn't work because the interaction that occurs between the square wave transitions and the sample rate introduces energy into the passband as a beat frequency. Yes, this interaction is aliasing, which is what we expect to get when sampling a signal that is not band-limited to our passband. That BLEP oscillator or other polyphase techniques are needed in order to synthesize a correctly sampled band-limited approximation of a square wave.
Similarly, if you're sampling an analog square wave with a high slew rate, you absolutely must have an analog anti-aliasing filter in front of your ADC, or you will end up with exactly the same beat frequencies in your sampled signal.
This is because the "naive" approach to e.g. a digital square wave is to sample a logically continuous function defined as:
Firstly, this is almost always going to be out of tune because the transitions from -1 to 1 and 1 to -1 necessarily fall across a digital sample boundary. For any frequency not evenly dividing the sample rate the transition will be slightly early or late.Secondly, the sharp rising edge of the square wave, from a "sum of sines" perspective, requires sinusoid components that are not part of a conventional square wave to represent them digitally. If you were to digitally sample an analog square wave through an ADC it would look quite different from the result of this function.
The naive digital sawtooth wave suffers from similar issues due to its discontinuity.